To enter code use command+enter
Basic calculations:
1+100
[1] 101
2^2
[1] 4
7/2
[1] 3.5
10-2
[1] 8
2*2
[1] 4
Order of operations matter: PEMDAS
3+5*2
[1] 13
(3+5)*2
[1] 16
Using scientific notation:
2e2
[1] 200
2/10000
[1] 2e-04
Can also use trigometric functions:
log(1)
[1] 0
exp(0)
[1] 1
You can also ask questions: T/F Statements
1 == 1
[1] TRUE
1 >=1
[1] TRUE
1 < 1
[1] FALSE
1 != 2
[1] TRUE
You can define objects/variables:
x <- 10
x
[1] 10
x+x
[1] 20
y <- x+x
y
[1] 20
Use the colon : to make a list:
1:5
[1] 1 2 3 4 5
Conducting operations on vectors:
2^(1:5)
[1] 2 4 8 16 32
Storing vector as an object:
v <- 1:5
2^v
[1] 2 4 8 16 32
Log transformations:
log10(v)
[1] 0.0000000 0.3010300 0.4771213 0.6020600 0.6989700
To see what is in current environment us ls() command:
ls()
[1] "ab" "AfricanLifeExpectancy" "Africas" "another_list" "avg.rand"
[6] "beer" "cats" "cor.gen" "df_example" "exp"
[11] "f" "f2" "forestdata" "g" "gapminder"
[16] "gapminder2" "gen.reg" "gp" "habitat.t2" "habitat.tgreater"
[21] "habitat.tless" "kigweught" "list_example" "log.left.bulb" "logweight"
[26] "m" "matrix_example" "n" "obs" "obs_fair"
[31] "obs_weighted" "p" "peds" "penguin_pca" "penguins"
[36] "penguins_matrix" "penguins_raw" "penguins2" "rand" "spearman.cor.gen"
[41] "spid.gen" "v" "x" "xlist" "y"
[46] "z"
To remove items from environment, use rm() command:
rm(x)
ls()
[1] "ab" "AfricanLifeExpectancy" "Africas" "another_list" "avg.rand"
[6] "beer" "cats" "cor.gen" "df_example" "exp"
[11] "f" "f2" "forestdata" "g" "gapminder"
[16] "gapminder2" "gen.reg" "gp" "habitat.t2" "habitat.tgreater"
[21] "habitat.tless" "kigweught" "list_example" "log.left.bulb" "logweight"
[26] "m" "matrix_example" "n" "obs" "obs_fair"
[31] "obs_weighted" "p" "peds" "penguin_pca" "penguins"
[36] "penguins_matrix" "penguins_raw" "penguins2" "rand" "spearman.cor.gen"
[41] "spid.gen" "v" "xlist" "y" "z"
To see what is already present, use command:
installed.packages()
Package LibPath Version
ade4 "ade4" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.7-19"
ambient "ambient" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.0.2"
AnnotationDbi "AnnotationDbi" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.58.0"
ape "ape" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "5.6-2"
aplot "aplot" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.1.8"
askpass "askpass" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.1"
assertthat "assertthat" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.2.1"
backports "backports" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.4.1"
base "base" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "4.2.1"
base64enc "base64enc" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.1-3"
BayesFactor "BayesFactor" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.9.12-4.4"
bayesm "bayesm" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "3.1-4"
bayestestR "bayestestR" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.13.0"
BH "BH" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.78.0-0"
Biobase "Biobase" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.56.0"
BiocFileCache "BiocFileCache" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.4.0"
BiocGenerics "BiocGenerics" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.42.0"
BiocIO "BiocIO" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.6.0"
BiocManager "BiocManager" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.30.18"
BiocParallel "BiocParallel" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.30.4"
BiocVersion "BiocVersion" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "3.15.2"
biomaRt "biomaRt" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.52.0"
biomformat "biomformat" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.24.0"
Biostrings "Biostrings" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.64.1"
bit "bit" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "4.0.4"
bit64 "bit64" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "4.0.5"
bitops "bitops" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.0-7"
blob "blob" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.2.3"
boot "boot" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.3-28"
brew "brew" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.0-8"
brio "brio" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.1.3"
broom "broom" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.0.1"
bslib "bslib" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.4.0"
cachem "cachem" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.0.6"
callr "callr" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "3.7.2"
cellranger "cellranger" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.1.0"
class "class" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "7.3-20"
classInt "classInt" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.4-8"
cli "cli" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "3.4.1"
clipr "clipr" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.8.0"
cluster "cluster" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.1.4"
coda "coda" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.19-4"
codetools "codetools" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.2-18"
colorspace "colorspace" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.0-3"
commonmark "commonmark" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.8.1"
compiler "compiler" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "4.2.1"
compositions "compositions" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.0-4"
contfrac "contfrac" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.1-12"
correlation "correlation" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.8.3"
cpp11 "cpp11" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.4.3"
crayon "crayon" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.5.2"
credentials "credentials" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.3.2"
curl "curl" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "4.3.3"
dada2 "dada2" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.24.0"
data.table "data.table" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.14.4"
datasets "datasets" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "4.2.1"
datawizard "datawizard" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.6.3"
DBI "DBI" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.1.3"
dbplyr "dbplyr" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.2.1"
DECIPHER "DECIPHER" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "2.24.0"
DelayedArray "DelayedArray" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "0.22.0"
deldir "deldir" "/Library/Frameworks/R.framework/Versions/4.2/Resources/library" "1.0-6"
Priority
ade4 NA
ambient NA
AnnotationDbi NA
ape NA
aplot NA
askpass NA
assertthat NA
backports NA
base "base"
base64enc NA
BayesFactor NA
bayesm NA
bayestestR NA
BH NA
Biobase NA
BiocFileCache NA
BiocGenerics NA
BiocIO NA
BiocManager NA
BiocParallel NA
BiocVersion NA
biomaRt NA
biomformat NA
Biostrings NA
bit NA
bit64 NA
bitops NA
blob NA
boot "recommended"
brew NA
brio NA
broom NA
bslib NA
cachem NA
callr NA
cellranger NA
class "recommended"
classInt NA
cli NA
clipr NA
cluster "recommended"
coda NA
codetools "recommended"
colorspace NA
commonmark NA
compiler "base"
compositions NA
contfrac NA
correlation NA
cpp11 NA
crayon NA
credentials NA
curl NA
dada2 NA
data.table NA
datasets "base"
datawizard NA
DBI NA
dbplyr NA
DECIPHER NA
DelayedArray NA
deldir NA
Depends
ade4 "R (>= 2.10)"
ambient "R (>= 3.0.2)"
AnnotationDbi "R (>= 2.7.0), methods, utils, stats4, BiocGenerics (>=\n0.29.2), Biobase (>= 1.17.0), IRanges"
ape "R (>= 3.2.0)"
aplot NA
askpass NA
assertthat NA
backports "R (>= 3.0.0)"
base NA
base64enc "R (>= 2.9.0)"
BayesFactor "R (>= 3.2.0), coda, Matrix (>= 1.1-1)"
bayesm "R (>= 3.2.0)"
bayestestR "R (>= 3.6)"
BH NA
Biobase "R (>= 2.10), BiocGenerics (>= 0.27.1), utils"
BiocFileCache "R (>= 3.4.0), dbplyr (>= 1.0.0)"
BiocGenerics "R (>= 4.0.0), methods, utils, graphics, stats"
BiocIO "R (>= 4.0)"
BiocManager NA
BiocParallel "methods, R (>= 3.5.0)"
BiocVersion "R (>= 4.2.0)"
biomaRt "methods"
biomformat "R (>= 3.2), methods"
Biostrings "R (>= 4.0.0), methods, BiocGenerics (>= 0.37.0), S4Vectors (>=\n0.27.12), IRanges (>= 2.30.1), XVector (>= 0.29.2),\nGenomeInfoDb"
bit "R (>= 2.9.2)"
bit64 "R (>= 3.0.1), bit (>= 4.0.0), utils, methods, stats"
bitops NA
blob NA
boot "R (>= 3.0.0), graphics, stats"
brew NA
brio NA
broom "R (>= 3.1)"
bslib "R (>= 2.10)"
cachem NA
callr "R (>= 3.4)"
cellranger "R (>= 3.0.0)"
class "R (>= 3.0.0), stats, utils"
classInt "R (>= 2.2)"
cli "R (>= 3.4)"
clipr NA
cluster "R (>= 3.5.0)"
coda "R (>= 2.14.0)"
codetools "R (>= 2.1)"
colorspace "R (>= 3.0.0), methods"
commonmark NA
compiler NA
compositions "R (>= 3.6)"
contfrac NA
correlation "R (>= 3.6)"
cpp11 NA
crayon NA
credentials NA
curl "R (>= 3.0.0)"
dada2 "R (>= 3.4.0), Rcpp (>= 0.12.0), methods (>= 3.4.0)"
data.table "R (>= 3.1.0)"
datasets NA
datawizard "R (>= 3.6)"
DBI "methods, R (>= 3.0.0)"
dbplyr "R (>= 3.1)"
DECIPHER "R (>= 3.5.0), Biostrings (>= 2.59.1), RSQLite (>= 1.1), stats,\nparallel"
DelayedArray "R (>= 4.0.0), methods, stats4, Matrix, BiocGenerics (>=\n0.37.0), MatrixGenerics (>= 1.1.3), S4Vectors (>= 0.27.2),\nIRanges (>= 2.17.3)"
deldir "R (>= 3.5.0)"
Imports
ade4 "graphics, grDevices, methods, stats, utils, MASS, pixmap, sp"
ambient "rlang, grDevices, graphics, stats"
AnnotationDbi "DBI, RSQLite, S4Vectors (>= 0.9.25), stats, KEGGREST"
ape "nlme, lattice, graphics, methods, stats, tools, utils,\nparallel, Rcpp (>= 0.12.0)"
aplot "ggfun (>= 0.0.6), ggplot2, ggplotify, patchwork, magrittr,\nmethods, utils"
askpass "sys (>= 2.1)"
assertthat "tools"
backports NA
base NA
base64enc NA
BayesFactor "pbapply, mvtnorm, stringr, utils, graphics, MatrixModels, Rcpp\n(>= 0.11.2), methods, hypergeo"
bayesm "Rcpp (>= 0.12.0), utils, stats, graphics, grDevices"
bayestestR "insight (>= 0.18.2), datawizard (>= 0.5.1), graphics, methods,\nstats, utils"
BH NA
Biobase "methods"
BiocFileCache "methods, stats, utils, dplyr, RSQLite, DBI, rappdirs,\nfilelock, curl, httr"
BiocGenerics "methods, utils, graphics, stats"
BiocIO "BiocGenerics, S4Vectors, methods, tools"
BiocManager "utils"
BiocParallel "stats, utils, futile.logger, parallel, snow, codetools"
BiocVersion NA
biomaRt "utils, XML (>= 3.99-0.7), AnnotationDbi, progress, stringr,\nhttr, digest, BiocFileCache, rappdirs, xml2"
biomformat "plyr (>= 1.8), jsonlite (>= 0.9.16), Matrix (>= 1.2), rhdf5"
Biostrings "methods, utils, grDevices, graphics, stats, crayon"
bit NA
bit64 NA
bitops NA
blob "methods, rlang, vctrs (>= 0.2.1)"
boot NA
brew NA
brio NA
broom "backports, dplyr (>= 1.0.0), ellipsis, generics (>= 0.0.2),\nglue, purrr, rlang, stringr, tibble (>= 3.0.0), tidyr (>=\n1.0.0), ggplot2"
bslib "grDevices, htmltools (>= 0.5.2), jsonlite, sass (>= 0.4.0),\njquerylib (>= 0.1.3), rlang, cachem, memoise"
cachem "rlang, fastmap"
callr "processx (>= 3.6.1), R6, utils"
cellranger "rematch, tibble"
class "MASS"
classInt "grDevices, stats, graphics, e1071, class, KernSmooth"
cli "utils"
clipr "utils"
cluster "graphics, grDevices, stats, utils"
coda "lattice"
codetools NA
colorspace "graphics, grDevices, stats"
commonmark NA
compiler NA
compositions "methods, utils, grDevices, stats, tensorA, robustbase, bayesm,\ngraphics, MASS"
contfrac NA
correlation "bayestestR (>= 0.13.0), datasets, datawizard (>= 0.5.1),\ninsight (>= 0.18.4), parameters (>= 0.18.2), stats"
cpp11 NA
crayon "grDevices, methods, utils"
credentials "openssl (>= 1.3), sys (>= 2.1), curl, jsonlite, askpass"
curl NA
dada2 "Biostrings (>= 2.42.1), ggplot2 (>= 2.1.0), reshape2 (>=\n1.4.1), ShortRead (>= 1.32.0), RcppParallel (>= 4.3.0),\nparallel (>= 3.2.0), IRanges (>= 2.6.0), XVector (>= 0.16.0),\nBiocGenerics (>= 0.22.0)"
data.table "methods"
datasets NA
datawizard "insight (>= 0.18.5), stats, utils"
DBI NA
dbplyr "assertthat (>= 0.2.0), blob (>= 1.2.0), cli (>= 3.3.0), DBI\n(>= 1.0.0), dplyr (>= 1.0.9), glue (>= 1.2.0), lifecycle (>=\n1.0.0), magrittr, methods, pillar (>= 1.5.0), purrr (>= 0.2.5),\nR6 (>= 2.2.2), rlang (>= 1.0.0), tibble (>= 1.4.2), tidyselect\n(>= 0.2.4), utils, vctrs (>= 0.4.1), withr"
DECIPHER "methods, DBI, S4Vectors, IRanges, XVector"
DelayedArray "stats"
deldir "graphics, grDevices"
LinkingTo
ade4 NA
ambient "cpp11 (>= 0.4.2)"
AnnotationDbi NA
ape "Rcpp"
aplot NA
askpass NA
assertthat NA
backports NA
base NA
base64enc NA
BayesFactor "Rcpp (>= 0.11.2), RcppEigen (>= 0.3.2.2.0)"
bayesm "Rcpp, RcppArmadillo"
bayestestR NA
BH NA
Biobase NA
BiocFileCache NA
BiocGenerics NA
BiocIO NA
BiocManager NA
BiocParallel "BH"
BiocVersion NA
biomaRt NA
biomformat NA
Biostrings "S4Vectors, IRanges, XVector"
bit NA
bit64 NA
bitops NA
blob NA
boot NA
brew NA
brio NA
broom NA
bslib NA
cachem NA
callr NA
cellranger NA
class NA
classInt NA
cli NA
clipr NA
cluster NA
coda NA
codetools NA
colorspace NA
commonmark NA
compiler NA
compositions NA
contfrac NA
correlation NA
cpp11 NA
crayon NA
credentials NA
curl NA
dada2 "Rcpp, RcppParallel"
data.table NA
datasets NA
datawizard NA
DBI NA
dbplyr NA
DECIPHER "Biostrings, S4Vectors, IRanges, XVector"
DelayedArray "S4Vectors"
deldir NA
Suggests
ade4 "ade4TkGUI, adegraphics, adephylo, ape, CircStats, deldir,\nlattice, spdep, splancs, waveslim, progress, foreach, parallel,\ndoParallel, iterators"
ambient "covr"
AnnotationDbi "hgu95av2.db, GO.db, org.Sc.sgd.db, org.At.tair.db, RUnit,\nTxDb.Hsapiens.UCSC.hg19.knownGene, org.Hs.eg.db, reactome.db,\nAnnotationForge, graph, EnsDb.Hsapiens.v75, BiocStyle, knitr"
ape "gee, expm, igraph, phangorn"
aplot "ggtree"
askpass "testthat"
assertthat "testthat, covr"
backports NA
base "methods"
base64enc NA
BayesFactor "doMC, foreach, testthat, knitr, markdown, arm, lme4, xtable,\nlanguageR"
bayesm "knitr, rmarkdown"
bayestestR "BayesFactor, bayesQR, bayesplot, BH, blavaan, bridgesampling,\nbrms, effectsize, emmeans, gamm4, ggdist, ggplot2, glmmTMB,\nhttr, KernSmooth, knitr, lavaan, lme4, logspline, MASS, mclust,\nmediation, modelbased, parameters, patchwork, performance,\nquadprog, posterior, RcppEigen, rmarkdown, rstan, rstanarm,\nsee, testthat, tweedie"
BH NA
Biobase "tools, tkWidgets, ALL, RUnit, golubEsets"
BiocFileCache "testthat, knitr, BiocStyle, rmarkdown, rtracklayer"
BiocGenerics "Biobase, S4Vectors, IRanges, GenomicRanges, DelayedArray,\nBiostrings, Rsamtools, AnnotationDbi, affy, affyPLM, DESeq2,\nflowClust, MSnbase, annotate, RUnit"
BiocIO "testthat, knitr, rmarkdown, BiocStyle"
BiocManager "BiocVersion, remotes, rmarkdown, testthat, withr, curl, knitr"
BiocParallel "BiocGenerics, tools, foreach, BatchJobs, BBmisc, doParallel,\nRmpi, GenomicRanges, RNAseqData.HNRNPC.bam.chr14,\nTxDb.Hsapiens.UCSC.hg19.knownGene, VariantAnnotation,\nRsamtools, GenomicAlignments, ShortRead, RUnit, BiocStyle,\nknitr, batchtools, data.table"
BiocVersion NA
biomaRt "BiocStyle, knitr, rmarkdown, testthat, mockery"
biomformat "testthat (>= 0.10), knitr (>= 1.10), BiocStyle (>= 1.6),\nrmarkdown (>= 0.7)"
Biostrings "BSgenome (>= 1.13.14), BSgenome.Celegans.UCSC.ce2 (>=\n1.3.11), BSgenome.Dmelanogaster.UCSC.dm3 (>= 1.3.11),\nBSgenome.Hsapiens.UCSC.hg18, drosophila2probe, hgu95av2probe,\nhgu133aprobe, GenomicFeatures (>= 1.3.14), hgu95av2cdf, affy\n(>= 1.41.3), affydata (>= 1.11.5), RUnit"
bit "testthat (>= 0.11.0), roxygen2, knitr, rmarkdown,\nmicrobenchmark, bit64 (>= 4.0.0), ff (>= 4.0.0)"
bit64 NA
bitops NA
blob "covr, crayon, pillar (>= 1.2.1), testthat"
boot "MASS, survival"
brew "testthat (>= 3.0.0)"
brio "covr, testthat (>= 2.1.0)"
broom "AER, AUC, bbmle, betareg, biglm, binGroup, boot, btergm (>=\n1.10.6), car, carData, caret, cluster, cmprsk, coda, covr, drc,\ne1071, emmeans, epiR, ergm (>= 3.10.4), fixest (>= 0.9.0), gam\n(>= 1.15), gee, geepack, glmnet, glmnetUtils, gmm, Hmisc,\nirlba, interp, joineRML, Kendall, knitr, ks, Lahman, lavaan,\nleaps, lfe, lm.beta, lme4, lmodel2, lmtest (>= 0.9.38),\nlsmeans, maps, maptools, margins, MASS, mclust, mediation,\nmetafor, mfx, mgcv, mlogit, modeldata, modeltests, muhaz,\nmultcomp, network, nnet, orcutt (>= 2.2), ordinal, plm, poLCA,\npsych, quantreg, rgeos, rmarkdown, robust, robustbase, rsample,\nsandwich, sp, spdep (>= 1.1), spatialreg, speedglm, spelling,\nsurvey, survival, systemfit, testthat (>= 2.1.0), tseries,\nvars, zoo"
bslib "shiny (>= 1.6.0), rmarkdown (>= 2.7), thematic, knitr,\ntestthat, withr, rappdirs, curl, magrittr"
cachem "testthat"
callr "cli (>= 1.1.0), covr, mockery, ps, rprojroot, spelling,\ntestthat (>= 3.0.0), withr (>= 2.3.0)"
cellranger "covr, testthat (>= 1.0.0), knitr, rmarkdown"
class NA
classInt "spData (>= 0.2.6.2), units, knitr, rmarkdown"
cli "callr, covr, digest, glue (>= 1.6.0), grDevices, htmltools,\nhtmlwidgets, knitr, methods, mockery, processx, ps (>=\n1.3.4.9000), rlang (>= 1.0.2.9003), rmarkdown, rprojroot,\nrstudioapi, testthat, tibble, whoami, withr"
clipr "covr, knitr, rmarkdown, rstudioapi (>= 0.5), testthat (>=\n2.0.0)"
cluster "MASS, Matrix"
coda NA
codetools NA
colorspace "datasets, utils, KernSmooth, MASS, kernlab, mvtnorm, vcd,\ntcltk, shiny, shinyjs, ggplot2, dplyr, scales, grid, png, jpeg,\nknitr, rmarkdown, RColorBrewer, rcartocolor, scico, viridis,\nwesanderson"
commonmark "curl, testthat, xml2"
compiler NA
compositions "rgl, combinat, energy, knitr, rmarkdown"
contfrac NA
correlation "BayesFactor, poorman, energy, ggplot2, ggraph, gt, Hmisc,\nknitr, lme4, mbend, polycor, ppcor, psych, rmarkdown, rmcorr,\nrstanarm, see, testthat (>= 3.1.0), tidygraph, wdm, WRS2"
cpp11 "bench, brio, callr, cli, covr, decor, desc, ggplot2, glue,\nknitr, lobstr, mockery, progress, rmarkdown, scales, Rcpp,\ntestthat, tibble, utils, vctrs, withr"
crayon "mockery, rstudioapi, testthat, withr"
credentials "testthat, knitr, rmarkdown"
curl "spelling, testthat (>= 1.0.0), knitr, jsonlite, rmarkdown,\nmagrittr, httpuv (>= 1.4.4), webutils"
dada2 "BiocStyle, knitr, rmarkdown"
data.table "bit64 (>= 4.0.0), bit (>= 4.0.4), curl, R.utils, xts,\nnanotime, zoo (>= 1.8-1), yaml, knitr, rmarkdown"
datasets NA
datawizard "bayestestR, boot, brms, data.table, dplyr (>= 1.0),\neffectsize, gamm4, ggplot2, haven, htmltools, httr, knitr,\nlme4, mediation, parameters, poorman (>= 0.2.6), psych, readxl,\nreadr, rio, rmarkdown, rstanarm, see, testthat (>= 3.1.0),\ntidyr, withr"
DBI "blob, covr, DBItest, dbplyr, downlit, dplyr, glue, hms,\nknitr, magrittr, RMariaDB, rmarkdown, rprojroot, RSQLite (>=\n1.1-2), testthat, xml2"
dbplyr "bit64, covr, knitr, Lahman, nycflights13, odbc, RMariaDB (>=\n1.0.2), rmarkdown, RPostgres (>= 1.1.3), RPostgreSQL, RSQLite\n(>= 2.1.0), testthat (>= 3.0.2), tidyr (>= 1.2.0)"
DECIPHER NA
DelayedArray "BiocParallel, HDF5Array (>= 1.17.12), genefilter,\nSummarizedExperiment, airway, lobstr, DelayedMatrixStats,\nknitr, rmarkdown, BiocStyle, RUnit"
deldir "polyclip"
Enhances License
ade4 NA "GPL (>= 2)"
ambient NA "MIT + file LICENSE"
AnnotationDbi NA "Artistic-2.0"
ape NA "GPL-2 | GPL-3"
aplot NA "Artistic-2.0"
askpass NA "MIT + file LICENSE"
assertthat NA "GPL-3"
backports NA "GPL-2 | GPL-3"
base NA "Part of R 4.2.1"
base64enc "png" "GPL-2 | GPL-3"
BayesFactor NA "GPL-2"
bayesm NA "GPL (>= 2)"
bayestestR NA "GPL-3"
BH NA "BSL-1.0"
Biobase NA "Artistic-2.0"
BiocFileCache NA "Artistic-2.0"
BiocGenerics NA "Artistic-2.0"
BiocIO NA "Artistic-2.0"
BiocManager NA "Artistic-2.0"
BiocParallel NA "GPL-2 | GPL-3"
BiocVersion NA "Artistic-2.0"
biomaRt NA "Artistic-2.0"
biomformat NA "GPL-2"
Biostrings "Rmpi" "Artistic-2.0"
bit NA "GPL-2 | GPL-3"
bit64 NA "GPL-2 | GPL-3"
bitops NA "GPL (>= 2)"
blob NA "MIT + file LICENSE"
boot NA "Unlimited"
brew NA "GPL-2"
brio NA "MIT + file LICENSE"
broom NA "MIT + file LICENSE"
bslib NA "MIT + file LICENSE"
cachem NA "MIT + file LICENSE"
callr NA "MIT + file LICENSE"
cellranger NA "MIT + file LICENSE"
class NA "GPL-2 | GPL-3"
classInt NA "GPL (>= 2)"
cli NA "MIT + file LICENSE"
clipr NA "GPL-3"
cluster NA "GPL (>= 2)"
coda NA "GPL (>= 2)"
codetools NA "GPL"
colorspace NA "BSD_3_clause + file LICENSE"
commonmark NA "BSD_2_clause + file LICENSE"
compiler NA "Part of R 4.2.1"
compositions NA "GPL (>= 2)"
contfrac NA "GPL-2"
correlation NA "GPL-3"
cpp11 NA "MIT + file LICENSE"
crayon NA "MIT + file LICENSE"
credentials NA "MIT + file LICENSE"
curl NA "MIT + file LICENSE"
dada2 NA "LGPL-2"
data.table NA "MPL-2.0 | file LICENSE"
datasets NA "Part of R 4.2.1"
datawizard NA "GPL (>= 3)"
DBI NA "LGPL (>= 2.1)"
dbplyr NA "MIT + file LICENSE"
DECIPHER NA "GPL-3"
DelayedArray NA "Artistic-2.0"
deldir NA "GPL (>= 2)"
License_is_FOSS License_restricts_use OS_type MD5sum NeedsCompilation Built
ade4 NA NA NA NA "yes" "4.2.0"
ambient NA NA NA NA "yes" "4.2.0"
AnnotationDbi NA NA NA NA "no" "4.2.0"
ape NA NA NA NA "yes" "4.2.0"
aplot NA NA NA NA "no" "4.2.0"
askpass NA NA NA NA "yes" "4.2.0"
assertthat NA NA NA NA "no" "4.2.0"
backports NA NA NA NA "yes" "4.2.0"
base NA NA NA NA NA "4.2.1"
base64enc NA NA NA NA "yes" "4.2.0"
BayesFactor NA NA NA NA "yes" "4.2.0"
bayesm NA NA NA NA "yes" "4.2.0"
bayestestR NA NA NA NA "no" "4.2.0"
BH NA NA NA NA "no" "4.2.0"
Biobase NA NA NA NA "yes" "4.2.0"
BiocFileCache NA NA NA NA "no" "4.2.0"
BiocGenerics NA NA NA NA "no" "4.2.0"
BiocIO NA NA NA NA "no" "4.2.0"
BiocManager NA NA NA NA "no" "4.2.0"
BiocParallel NA NA NA NA "yes" "4.2.1"
BiocVersion NA NA NA NA "no" "4.2.0"
biomaRt NA NA NA NA "no" "4.2.0"
biomformat NA NA NA NA "no" "4.2.0"
Biostrings NA NA NA NA "yes" "4.2.1"
bit NA NA NA NA "yes" "4.2.0"
bit64 NA NA NA NA "yes" "4.2.0"
bitops NA NA NA NA "yes" "4.2.0"
blob NA NA NA NA "no" "4.2.0"
boot NA NA NA NA "no" "4.2.1"
brew NA NA NA NA "no" "4.2.0"
brio NA NA NA NA "yes" "4.2.0"
broom NA NA NA NA "no" "4.2.0"
bslib NA NA NA NA "no" "4.2.0"
cachem NA NA NA NA "yes" "4.2.0"
callr NA NA NA NA "no" "4.2.0"
cellranger NA NA NA NA "no" "4.2.0"
class NA NA NA NA "yes" "4.2.1"
classInt NA NA NA NA "yes" "4.2.0"
cli NA NA NA NA "yes" "4.2.0"
clipr NA NA NA NA "no" "4.2.0"
cluster NA NA NA NA "yes" "4.2.0"
coda NA NA NA NA "no" "4.2.0"
codetools NA NA NA NA "no" "4.2.1"
colorspace NA NA NA NA "yes" "4.2.0"
commonmark NA NA NA NA "yes" "4.2.0"
compiler NA NA NA NA NA "4.2.1"
compositions NA NA NA NA "yes" "4.2.0"
contfrac NA NA NA NA "yes" "4.2.0"
correlation NA NA NA NA "no" "4.2.0"
cpp11 NA NA NA NA "no" "4.2.1"
crayon NA NA NA NA "no" "4.2.0"
credentials NA NA NA NA "no" "4.2.0"
curl NA NA NA NA "yes" "4.2.0"
dada2 NA NA NA NA "yes" "4.2.0"
data.table NA NA NA NA "yes" "4.2.1"
datasets NA NA NA NA NA "4.2.1"
datawizard NA NA NA NA "no" "4.2.0"
DBI NA NA NA NA "no" "4.2.0"
dbplyr NA NA NA NA "no" "4.2.0"
DECIPHER NA NA NA NA "yes" "4.2.0"
DelayedArray NA NA NA NA "yes" "4.2.0"
deldir NA NA NA NA "yes" "4.2.0"
[ reached getOption("max.print") -- omitted 257 rows ]
installed.packages("vegan", dependencies=TRUE)
Package LibPath Version Priority Depends Imports LinkingTo Suggests Enhances License License_is_FOSS License_restricts_use
OS_type Archs MD5sum NeedsCompilation Built
If you don’t know what a command does, just place a question mark (?) infront of the command:
?plot
You can also use the help command:
help()
You can even use it with symbols:
#? <-
?"<-"
Can get vignette:
vignette("FAQ-vegan")
To get citations:
citation("vegan")
To cite package ‘vegan’ in publications use:
Oksanen J, Simpson G, Blanchet F, Kindt R, Legendre P, Minchin P, O'Hara R, Solymos P, Stevens M, Szoecs E, Wagner H,
Barbour M, Bedward M, Bolker B, Borcard D, Carvalho G, Chirico M, De Caceres M, Durand S, Evangelista H, FitzJohn R,
Friendly M, Furneaux B, Hannigan G, Hill M, Lahti L, McGlinn D, Ouellette M, Ribeiro Cunha E, Smith T, Stier A, Ter Braak
C, Weedon J (2022). _vegan: Community Ecology Package_. R package version 2.6-2,
<https://CRAN.R-project.org/package=vegan>.
A BibTeX entry for LaTeX users is
@Manual{,
title = {vegan: Community Ecology Package},
author = {Jari Oksanen and Gavin L. Simpson and F. Guillaume Blanchet and Roeland Kindt and Pierre Legendre and Peter R. Minchin and R.B. O'Hara and Peter Solymos and M. Henry H. Stevens and Eduard Szoecs and Helene Wagner and Matt Barbour and Michael Bedward and Ben Bolker and Daniel Borcard and Gustavo Carvalho and Michael Chirico and Miquel {De Caceres} and Sebastien Durand and Heloisa Beatriz Antoniazi Evangelista and Rich FitzJohn and Michael Friendly and Brendan Furneaux and Geoffrey Hannigan and Mark O. Hill and Leo Lahti and Dan McGlinn and Marie-Helene Ouellette and Eduardo {Ribeiro Cunha} and Tyler Smith and Adrian Stier and Cajo J.F. {Ter Braak} and James Weedon},
year = {2022},
note = {R package version 2.6-2},
url = {https://CRAN.R-project.org/package=vegan},
}
data.frame makes a spreadsheet (like in python)
cats <- data.frame(coat = c("calico", "black", "tabby"), weight = c(2.1,5,3.2), likes_string = c(1,0,1))
cats
getwd()
[1] "/Users/maggieshostak/Desktop/Practical Computing/Notes"
To create a new column, use cbind:
logweight <- log(cats$weight)
cbind(cats,logweight)
To print a string of text, use “paste” commmand:
paste("My cat is ", cats$coat, ", and it weighs ", cats$weight, " kg.", sep = "")
[1] "My cat is calico, and it weighs 2.1 kg." "My cat is black, and it weighs 5 kg." "My cat is tabby, and it weighs 3.2 kg."
typeof(cats$coat)
[1] "character"
typeof(cats$weight)
[1] "double"
A double represent a numver with decimal places
typeof(cats$likes_string)
[1] "double"
typeof(3.14159)
[1] "double"
typeof(1i)
[1] "complex"
You can use “TRUE” & “FALSE”
typeof(TRUE)
[1] "logical"
typeof(T)
[1] "logical"
typeof(FALSE)
[1] "logical"
typeof(F)
[1] "logical"
typeof(cats)
[1] "list"
class(cats)
[1] "data.frame"
To open a file use:
file.show()
To make 1=TRUE & 0=FALSE
cats$likes_string <- as.logical(cats$likes_string)
typeof(cats$likes_string)
[1] "logical"
cats$likes_string
[1] TRUE FALSE TRUE
c("a", "b")
[1] "a" "b"
ab <-c("a","b")
ab
[1] "a" "b"
c(ab, "c")
[1] "a" "b" "c"
c(ab, ab)
[1] "a" "b" "a" "b"
To get a range, just use a colon (:)
1:10
[1] 1 2 3 4 5 6 7 8 9 10
You can also use seq(#):
seq(10)
[1] 1 2 3 4 5 6 7 8 9 10
z <- seq(10)
head(z, n=3)
[1] 1 2 3
length(z)
[1] 10
class(z)
[1] "integer"
typeof(z)
[1] "integer"
seq(1,100, by=5)
[1] 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
Gives you 1 to 100 in intervals of 5
seq(70,100, by=5)
[1] 70 75 80 85 90 95 100
cats <- cbind(cats,logweight)
cats
str(cats$coat)
chr [1:3] "calico" "black" "tabby"
str(cats$coat)
chr [1:3] "calico" "black" "tabby"
coats <- "tabby", "tortoiseshell" "tortoiseshell", "black", "tabby"
Error: unexpected ',' in "coats <- "tabby","
factor("coats")
[1] coats
Levels: coats
categories <- factor(coats)
Error in factor(coats) : object 'coats' not found
list_example <- list(title="Numbers", numbers=1:10, data=TRUE)
list_example
$title
[1] "Numbers"
$numbers
[1] 1 2 3 4 5 6 7 8 9 10
$data
[1] TRUE
another_list <- list(1,"a", TRUE, 1+1i)
another_list
[[1]]
[1] 1
[[2]]
[1] "a"
[[3]]
[1] TRUE
[[4]]
[1] 1+1i
typeof(list_example)
[1] "list"
class(list_example)
[1] "list"
typeof(another_list)
[1] "list"
class(another_list)
[1] "list"
typeof(cats)
[1] "list"
class(cats)
[1] "data.frame"
To turrn lists -> data.frame
data.frame(list_example)
data.frame(another_list)
To subset lists, # after comma means look at colomuns, # before means look at row:
cats
cats[,3]
[1] TRUE FALSE TRUE
cats[3,]
cats[2:3,c(1,3)]
Matrix: made for math, all numbers
Data.frame/spreadsheet: can be quantitative or qualitative
matrix_example <- matrix(0,ncol=5, nrow=3)
matrix_example
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
class(matrix_example)
[1] "matrix" "array"
typeof(matrix_example)
[1] "double"
str(matrix_example)
num [1:3, 1:5] 0 0 0 0 0 0 0 0 0 0 ...
dim(matrix_example)
[1] 3 5
ncol(matrix_example)
[1] 5
nrow(matrix_example)
[1] 3
To turn a matrix into a spreadsheet
class(data.frame(matrix_example))
[1] "data.frame"
To save as a data.frame
df_example <- data.frame(matrix_example)
df_example
Works with dataframes & matrices
p
[1] 2.3 6.9 4.0 23.0 1.0
TO apply names to vector use:
names(p) <- c('a', 'b', 'c', 'd', 'e')
p
a b c d e
2.3 6.9 4.0 23.0 1.0
p[1]
a
2.3
p[2:4]
b c d
6.9 4.0 23.0
p[c(1,5)]
a e
2.3 1.0
p[c(1,1,1,3,4,5)]
a a a c d e
2.3 2.3 2.3 4.0 23.0 1.0
To remove columns, use a (-)
p[-3]
a b d e
2.3 6.9 23.0 1.0
p[c(-1,-5)]
b c d
6.9 4.0 23.0
p[-(2:4)]
a e
2.3 1.0
p[c('a','c')]
a c
2.3 4.0
To say not equal to, use !=
p[names(p) !='c']
a b d e
2.3 6.9 23.0 1.0
f <- factor(c('a', 'b', 'c','d', 'e'))
f
[1] a b c d e
Levels: a b c d e
To do absolutely is equal to, use double =’s
f[f == 'a']
[1] a
Levels: a b c d e
f[1:3]
[1] a b c
Levels: a b c d e
To see if the factor contains some option, use %in%
f[f %in% c('b','c')]
[1] b c
Levels: a b c d e
f[-3]
[1] a b d e
Levels: a b c d e
f2
[1] a a b c c
Levels: a b c
f2[f2 == 'a']
[1] a a
Levels: a b c
f2 [f2 %in% c('a','c')]
[1] a a c c
Levels: a b c
Setting the seed will make sure you get the same result everytime.
set.seed(65)
m <- matrix(rnorm(6*4), ncol=4, nrow=6)
m
[,1] [,2] [,3]
[1,] -1.1968205 -0.3943568 0.4232027
[2,] -0.9516674 -1.2484030 -1.8461725
[3,] 0.2786364 -0.6521289 -0.2456747
[4,] -1.4449411 -1.1707346 1.2538299
[5,] 0.8216518 0.3604497 1.5633419
[6,] 1.2470188 1.2302708 -1.2644661
[,4]
[1,] -2.34367583
[2,] -0.77605977
[3,] -2.16173144
[4,] 0.05701057
[5,] -0.24867199
[6,] -0.07830796
For specific rows
m[3:4, c(3,1)]
[,1] [,2]
[1,] -0.2456747 0.2786364
[2,] 1.2538299 -1.4449411
For specific columns
m[,c(3,4)]
[,1] [,2]
[1,] 0.4232027 -2.34367583
[2,] -1.8461725 -0.77605977
[3,] -0.2456747 -2.16173144
[4,] 1.2538299 0.05701057
[5,] 1.5633419 -0.24867199
[6,] -1.2644661 -0.07830796
xlist <- list(a="BIOL48006220", b=seq(1,10, by = 0.5), data="Grades")
xlist
$a
[1] "BIOL48006220"
$b
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
[9] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5
[17] 9.0 9.5 10.0
$data
[1] "Grades"
by = 0.5 is step size/increment
xlist[1:2]
$a
[1] "BIOL48006220"
$b
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
[9] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5
[17] 9.0 9.5 10.0
xlist['a']
$a
[1] "BIOL48006220"
You can also subslice by using [[]] to represent just the value not the object
xlist[['a']]
[1] "BIOL48006220"
xlist['b']
$b
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
[9] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5
[17] 9.0 9.5 10.0
xlist[['b']]
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
[9] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5
[17] 9.0 9.5 10.0
You can use a $ to represent what ever the values are:
xlist$b
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
[9] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5
[17] 9.0 9.5 10.0
gp <- read.csv('/Users/maggieshostak/Desktop/Practical\ Computing/Class\ Examples/PracticalComputing_2022/data/forest_area_sq_km.csv')
head(gp, n=10L)
To determine how many rows
nrow(gp)
[1] 214
head(gp[["country"]], 10L)
[1] "Aruba"
[2] "Afghanistan"
[3] "Angola"
[4] "Albania"
[5] "Andorra"
[6] "United Arab Emirates"
[7] "Argentina"
[8] "Armenia"
[9] "American Samoa"
[10] "Antigua and Barbuda"
gp$X1997
[1] "4.2" "12.1k" "780k" "7730"
[5] "160" "2970" "337k" "3330"
[9] "178" "95.8" "1.32M" "38.3k"
[13] "9790" "2100" "7560" "42.8k"
[17] "73.2k" "19.2k" "33.7k" "3.4"
[21] "5100" "21.3k" "81.7k" "14.9k"
[25] "10" "556k" "5.59M" "63"
[29] "4000" "25.9k" "179k" "230k"
[33] "3.48M" "8.3" "11.9k" "157k"
[37] "1.73M" "56.5k" "218k" "1.45M"
[41] "222k" "632k" "426" "349"
[45] "28.7k" "23.6k" "130" "1700"
[49] "26.4k" "113k" "56" "484"
[53] "5640" "19k" "16k" "139k"
[57] "561" "11.2k" "165k" "22.3k"
[61] "187k" "223k" "9930" "151k"
[65] "0.8" "638" "237k" "34.6"
[69] "29.2k" "27.6k" "90.6k" "0"
[73] "70k" "3690" "21.7k" "26.3k"
[77] "35.4k" "177" "2.2" "43.2k"
[81] "240" "186k" "68.2k" "18.8k"
[85] "3810" "19k" "1.05M" "669k"
[89] "5970" "92.8k" "8150" "273"
[93] "1490" "82.1k" "5210" "975"
[97] "249k" "31.6k" "39.4k" "11.7k"
[101] "108k" "11.8" "110" "64.9k"
[105] "45.7" "175k" "1380" "82.8k"
[109] "2170" "211" "66.6" "22k"
[113] "345" "20.1k" "870" "32.3k"
[117] "10" "55k" "0" "3410"
[121] "132k" "8.2" "688k" "94"
[125] "9480" "133k" "3.5" "357k"
[129] "6260" "143k" "323" "416k"
[133] "4330" "418" "31.7k" "199k"
[137] "82k" "8370" "14.5k" "252k"
[141] "56k" "3570" "" "121k"
[145] "57.6k" "0" "97.5k" "30"
[149] "46.1k" "44.8k" "755k" "74k"
[153] "393" "363k" "90.2k" "4070"
[157] "65.5k" "33k" "235k" "90.8"
[161] "1480" "0" "63.7k" "8.09M"
[165] "2930" "9770" "" "89.4k"
[169] "166" "25.4k" "29.7k" "6830"
[173] "10" "76.7k" "24.8k" ""
[177] "584" "153k" "19k" "12.2k"
[181] "281k" "4710" "" "337"
[185] "4200" "105" "64.3k" "12.9k"
[189] "191k" "4100" "41.3k" "9520"
[193] "89.5" "2380" "6630" "201k"
[197] "10" "544k" "32.5k" "94.6k"
[201] "12.5k" "3.03M" "28.8k" "283"
[205] "497k" "36.8" "213" "113k"
[209] "4420" "1720" "5490" "179k"
[213] "471k" "185k"
To just look at rows 1-3 & columns 2-5
To just look at columns 1 & 3 & 4 & 5
To indent we use { then press enter to make conditional statement:
print("n is equal to 10")
[1] "n is equal to 10"
for (n in seq(1,20)) {
if (n<10) {
print("n is less than 10")
} else if (n>10) {
print("n is more than 10")
} else {
print("n is equal to 10")
}
}
[1] "n is less than 10"
[1] "n is less than 10"
[1] "n is less than 10"
[1] "n is less than 10"
[1] "n is less than 10"
[1] "n is less than 10"
[1] "n is less than 10"
[1] "n is less than 10"
[1] "n is less than 10"
[1] "n is equal to 10"
[1] "n is more than 10"
[1] "n is more than 10"
[1] "n is more than 10"
[1] "n is more than 10"
[1] "n is more than 10"
[1] "n is more than 10"
[1] "n is more than 10"
[1] "n is more than 10"
[1] "n is more than 10"
[1] "n is more than 10"
Paste is like the cat command:
g <- 0
while (g <=10) {
print(paste(g, "is less than or equal to 10"))
g <- g+1
}
[1] "0 is less than or equal to 10"
[1] "1 is less than or equal to 10"
[1] "2 is less than or equal to 10"
[1] "3 is less than or equal to 10"
[1] "4 is less than or equal to 10"
[1] "5 is less than or equal to 10"
[1] "6 is less than or equal to 10"
[1] "7 is less than or equal to 10"
[1] "8 is less than or equal to 10"
[1] "9 is less than or equal to 10"
[1] "10 is less than or equal to 10"
installed.packages('ggplot2')
Package LibPath Version Priority
Depends Imports LinkingTo Suggests
Enhances License License_is_FOSS
License_restricts_use OS_type Archs
MD5sum NeedsCompilation Built
library(ggplot2)
gp <- read.csv('/Users/maggieshostak/Desktop/Practical\ Computing/Class\ Examples/PracticalComputing_2022/data/gapminder_all.csv')
head(gp)
ggplot(data=gp, mapping=aes(x=gdpPercap_1952, y=pop_1952)) + geom_point()
gapminder <- read.csv("https://raw.githubusercontent.com/swcarpentry/r-novice-gapminder/gh-pages/_episodes_rmd/data/gapminder_data.csv")
head(gapminder)
ggplot(data=gapminder, mapping=aes(x=gdpPercap, y=lifeExp)) + geom_point()
ggplot(data=gapminder, mapping=aes(x=year, y=lifeExp, by=country, color=continent)) + geom_line()
To transfer an exponential model, use the log10:
Africas <- gapminder[gapminder$continent == "Africa", ]
head(Africas)
NA
Facet_wrap: tells you how you want to plot them independetly
ggplot(data=Africas, mapping=aes(x=year, y=lifeExp)) +
geom_line() +
facet_wrap(~ country)
How to save a graph to make an output file:
AfricanLifeExpectancy <- ggplot(data=Africas, mapping=aes(x=year, y=lifeExp)) +
geom_line(color="red") +
facet_wrap(~ country) +
theme(axis.text.x = element_text(angle=90)) +
labs(
x="Year",
Y="lifeExp",
Title="Life Expectency Over Time in African Countries",
)
ggsave(filename="data/AfricanLifeExpectancy.png", plot=AfricanLifeExpectancy, width=24, height=40, dpi=300, units="cm")
pdf(file="results/AfricanLifeExp.pdf", width=24, height=40)
Error in pdf(file = "results/AfricanLifeExp.pdf", width = 24, height = 40) :
cannot open file 'results/AfricanLifeExp.pdf'
write.table(gapminder, file="data/gapminder_web.csv", sep=",")
write.csv(Africas, file="data/gapmminder_web_africas.csv")
install.packages(c("ggridges", "viridis", "hrbrthemes", dependencies=T))
Warning in install.packages :
package ‘TRUE’ is not available for this version of R
A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
also installing the dependencies ‘gridExtra’, ‘gdtools’
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/gridExtra_2.3.tgz'
Content type 'application/x-gzip' length 1103175 bytes (1.1 MB)
==================================================
downloaded 1.1 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/gdtools_0.2.4.tgz'
Content type 'application/x-gzip' length 1921424 bytes (1.8 MB)
==================================================
downloaded 1.8 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/ggridges_0.5.4.tgz'
Content type 'application/x-gzip' length 2254162 bytes (2.1 MB)
==================================================
downloaded 2.1 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/viridis_0.6.2.tgz'
Content type 'application/x-gzip' length 2998780 bytes (2.9 MB)
==================================================
downloaded 2.9 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/hrbrthemes_0.8.0.tgz'
Content type 'application/x-gzip' length 2291025 bytes (2.2 MB)
==================================================
downloaded 2.2 MB
The downloaded binary packages are in
/var/folders/jq/7jw4lmyx39n4m1jf7hb6v0t80000gn/T//RtmprpsOn3/downloaded_packages
library(hrbrthemes)
Registered S3 methods overwritten by 'htmltools':
method from
print.html tools:rstudio
print.shiny.tag tools:rstudio
print.shiny.tag.list tools:rstudio
NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
hrbrthemes::import_roboto_condensed()
sh: line 1: 2974 Segmentation fault: 11 '/Library/Frameworks/R.framework/Versions/4.2/Resources/library/Rttf2pt1/exec//ttf2pt1' -a -GfAe '/Library/Frameworks/R.framework/Versions/4.2/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Bold.ttf' '/var/folders/jq/7jw4lmyx39n4m1jf7hb6v0t80000gn/T//RtmprpsOn3/fonts/RobotoCondensed-Bold' 2>&1
sh: line 1: 2977 Segmentation fault: 11 '/Library/Frameworks/R.framework/Versions/4.2/Resources/library/Rttf2pt1/exec//ttf2pt1' -a -GfAe '/Library/Frameworks/R.framework/Versions/4.2/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Light.ttf' '/var/folders/jq/7jw4lmyx39n4m1jf7hb6v0t80000gn/T//RtmprpsOn3/fonts/RobotoCondensed-Light' 2>&1
sh: line 1: 2979 Segmentation fault: 11 '/Library/Frameworks/R.framework/Versions/4.2/Resources/library/Rttf2pt1/exec//ttf2pt1' -a -GfAe '/Library/Frameworks/R.framework/Versions/4.2/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Regular.ttf' '/var/folders/jq/7jw4lmyx39n4m1jf7hb6v0t80000gn/T//RtmprpsOn3/fonts/RobotoCondensed-Regular' 2>&1
You will likely need to install these fonts on your system as well.
You can find them in [/Library/Frameworks/R.framework/Versions/4.2/Resources/library/hrbrthemes/fonts/roboto-condensed]
..x.. makes it a gradient fill & smooth
ggplot(lincoln_weather, aes(x=`Mean Temperature[F]`, y= `Month`, fill=..x..)) + geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) + scale_fill_viridis(name="temp. [F]", option = "A") + theme_ipsum() +
theme(
legend.position="none",
panel.spacing = unit(0.1, "lines"),
strip.text.x = element_text(size=8))
Error in ggplot(lincoln_weather, aes(x = `Mean Temperature[F]`, y = Month, :
could not find function "ggplot"
Themes dictate fonts
Taking family and year described, use bin to make a hard/bar plot
install.packages(c("ggstatsplot", "palmerpenguins", dependencies=T))
Warning in install.packages :
package ‘TRUE’ is not available for this version of R
A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
also installing the dependencies ‘elliptic’, ‘contfrac’, ‘deSolve’, ‘coda’, ‘pbapply’, ‘mvtnorm’, ‘MatrixModels’, ‘hypergeo’, ‘reshape’, ‘mc2d’, ‘bayestestR’, ‘prismatic’, ‘BayesFactor’, ‘effectsize’, ‘WRS2’, ‘zeallot’, ‘correlation’, ‘datawizard’, ‘ggrepel’, ‘ggsignif’, ‘insight’, ‘paletteer’, ‘parameters’, ‘performance’, ‘statsExpressions’
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/elliptic_1.4-0.tgz'
Content type 'application/x-gzip' length 1257923 bytes (1.2 MB)
==================================================
downloaded 1.2 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/contfrac_1.1-12.tgz'
Content type 'application/x-gzip' length 27492 bytes (26 KB)
==================================================
downloaded 26 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/deSolve_1.34.tgz'
Content type 'application/x-gzip' length 2619125 bytes (2.5 MB)
==================================================
downloaded 2.5 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/coda_0.19-4.tgz'
Content type 'application/x-gzip' length 321854 bytes (314 KB)
==================================================
downloaded 314 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/pbapply_1.5-0.tgz'
Content type 'application/x-gzip' length 76335 bytes (74 KB)
==================================================
downloaded 74 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/mvtnorm_1.1-3.tgz'
Content type 'application/x-gzip' length 260575 bytes (254 KB)
==================================================
downloaded 254 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/MatrixModels_0.5-1.tgz'
Content type 'application/x-gzip' length 435122 bytes (424 KB)
==================================================
downloaded 424 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/hypergeo_1.2-13.tgz'
Content type 'application/x-gzip' length 352373 bytes (344 KB)
==================================================
downloaded 344 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/reshape_0.8.9.tgz'
Content type 'application/x-gzip' length 168752 bytes (164 KB)
==================================================
downloaded 164 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/mc2d_0.1-21.tgz'
Content type 'application/x-gzip' length 1378438 bytes (1.3 MB)
==================================================
downloaded 1.3 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/bayestestR_0.13.0.tgz'
Content type 'application/x-gzip' length 1477700 bytes (1.4 MB)
==================================================
downloaded 1.4 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/prismatic_1.1.1.tgz'
Content type 'application/x-gzip' length 786489 bytes (768 KB)
==================================================
downloaded 768 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/BayesFactor_0.9.12-4.4.tgz'
Content type 'application/x-gzip' length 4278037 bytes (4.1 MB)
==================================================
downloaded 4.1 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/effectsize_0.8.1.tgz'
Content type 'application/x-gzip' length 680142 bytes (664 KB)
==================================================
downloaded 664 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/WRS2_1.1-4.tgz'
Content type 'application/x-gzip' length 965147 bytes (942 KB)
==================================================
downloaded 942 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/zeallot_0.1.0.tgz'
Content type 'application/x-gzip' length 57961 bytes (56 KB)
==================================================
downloaded 56 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/correlation_0.8.3.tgz'
Content type 'application/x-gzip' length 2528105 bytes (2.4 MB)
==================================================
downloaded 2.4 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/datawizard_0.6.3.tgz'
Content type 'application/x-gzip' length 1142513 bytes (1.1 MB)
==================================================
downloaded 1.1 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/ggrepel_0.9.1.tgz'
Content type 'application/x-gzip' length 710937 bytes (694 KB)
==================================================
downloaded 694 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/ggsignif_0.6.4.tgz'
Content type 'application/x-gzip' length 608262 bytes (594 KB)
==================================================
downloaded 594 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/insight_0.18.6.tgz'
Content type 'application/x-gzip' length 2004384 bytes (1.9 MB)
==================================================
downloaded 1.9 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/paletteer_1.5.0.tgz'
Content type 'application/x-gzip' length 432181 bytes (422 KB)
==================================================
downloaded 422 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/parameters_0.19.0.tgz'
Content type 'application/x-gzip' length 1861775 bytes (1.8 MB)
==================================================
downloaded 1.8 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/performance_0.10.0.tgz'
Content type 'application/x-gzip' length 3061327 bytes (2.9 MB)
==================================================
downloaded 2.9 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/statsExpressions_1.3.4.tgz'
Content type 'application/x-gzip' length 3392521 bytes (3.2 MB)
==================================================
downloaded 3.2 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/ggstatsplot_0.9.5.tgz'
Content type 'application/x-gzip' length 3422071 bytes (3.3 MB)
==================================================
downloaded 3.3 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/palmerpenguins_0.1.1.tgz'
Content type 'application/x-gzip' length 3003192 bytes (2.9 MB)
==================================================
downloaded 2.9 MB
The downloaded binary packages are in
/var/folders/jq/7jw4lmyx39n4m1jf7hb6v0t80000gn/T//RtmprpsOn3/downloaded_packages
library(ggstatsplot)
You can cite this package as:
Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
Journal of Open Source Software, 6(61), 3167, doi:10.21105/joss.03167
data("penguins", package = "palmerpenguins")
penguins
penguins2 <- drop_na(penguins)
penguins2
plt <- ggbetweenstats(
data = penguins,
x=species,
y=bill length)
Error: unexpected symbol in:
" x=species,
y=bill length"
gapminder2 <- drop_na(gapminder)
boxplot(gapminder2$lifeExp ~ gapminder2$continent)
Box plots don’t show distribution, instead use a violin plot
install.packages("vioplot", dependencies = T)
also installing the dependencies ‘sm’, ‘zoo’
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/sm_2.2-5.7.1.tgz'
Content type 'application/x-gzip' length 814173 bytes (795 KB)
==================================================
downloaded 795 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/zoo_1.8-11.tgz'
Content type 'application/x-gzip' length 1017258 bytes (993 KB)
==================================================
downloaded 993 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/vioplot_0.3.7.tgz'
Content type 'application/x-gzip' length 822536 bytes (803 KB)
==================================================
downloaded 803 KB
The downloaded binary packages are in
/var/folders/jq/7jw4lmyx39n4m1jf7hb6v0t80000gn/T//RtmprpsOn3/downloaded_packages
library(vioplot)
Loading required package: sm
Package 'sm', version 2.2-5.7: type help(sm) for summary information
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
plt <- ggbetweenstats()
Error in assign(name, val, envir = env) :
argument "val" is missing, with no default
To save/export graph
ggsave(
flename = "insert name",
plot = plt
width = 8
Error: unexpected symbol in:
" plot = plt
width"
You can then load the stuff in InkScape to adjust the visual aesthetics
For principle components, you need a matrix
penguins_matrix
bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
[1,] 39.1 18.7 181 3750
[2,] 39.5 17.4 186 3800
[3,] 40.3 18.0 195 3250
[4,] 36.7 19.3 193 3450
[5,] 39.3 20.6 190 3650
[6,] 38.9 17.8 181 3625
[7,] 39.2 19.6 195 4675
[8,] 41.1 17.6 182 3200
[9,] 38.6 21.2 191 3800
[10,] 34.6 21.1 198 4400
[11,] 36.6 17.8 185 3700
[12,] 38.7 19.0 195 3450
[13,] 42.5 20.7 197 4500
[14,] 34.4 18.4 184 3325
[15,] 46.0 21.5 194 4200
[16,] 37.8 18.3 174 3400
[17,] 37.7 18.7 180 3600
[18,] 35.9 19.2 189 3800
[19,] 38.2 18.1 185 3950
[20,] 38.8 17.2 180 3800
[21,] 35.3 18.9 187 3800
[22,] 40.6 18.6 183 3550
[23,] 40.5 17.9 187 3200
[24,] 37.9 18.6 172 3150
[25,] 40.5 18.9 180 3950
[26,] 39.5 16.7 178 3250
[27,] 37.2 18.1 178 3900
[28,] 39.5 17.8 188 3300
[29,] 40.9 18.9 184 3900
[30,] 36.4 17.0 195 3325
[31,] 39.2 21.1 196 4150
[32,] 38.8 20.0 190 3950
[33,] 42.2 18.5 180 3550
[34,] 37.6 19.3 181 3300
[35,] 39.8 19.1 184 4650
[36,] 36.5 18.0 182 3150
[37,] 40.8 18.4 195 3900
[38,] 36.0 18.5 186 3100
[39,] 44.1 19.7 196 4400
[40,] 37.0 16.9 185 3000
[41,] 39.6 18.8 190 4600
[42,] 41.1 19.0 182 3425
[43,] 36.0 17.9 190 3450
[44,] 42.3 21.2 191 4150
[45,] 39.6 17.7 186 3500
[46,] 40.1 18.9 188 4300
[47,] 35.0 17.9 190 3450
[48,] 42.0 19.5 200 4050
[49,] 34.5 18.1 187 2900
[50,] 41.4 18.6 191 3700
[51,] 39.0 17.5 186 3550
[52,] 40.6 18.8 193 3800
[53,] 36.5 16.6 181 2850
[54,] 37.6 19.1 194 3750
[55,] 35.7 16.9 185 3150
[56,] 41.3 21.1 195 4400
[57,] 37.6 17.0 185 3600
[58,] 41.1 18.2 192 4050
[59,] 36.4 17.1 184 2850
[60,] 41.6 18.0 192 3950
[61,] 35.5 16.2 195 3350
[62,] 41.1 19.1 188 4100
[63,] 35.9 16.6 190 3050
[64,] 41.8 19.4 198 4450
[65,] 33.5 19.0 190 3600
[66,] 39.7 18.4 190 3900
[67,] 39.6 17.2 196 3550
[68,] 45.8 18.9 197 4150
[69,] 35.5 17.5 190 3700
[70,] 42.8 18.5 195 4250
[71,] 40.9 16.8 191 3700
[72,] 37.2 19.4 184 3900
[73,] 36.2 16.1 187 3550
[74,] 42.1 19.1 195 4000
[75,] 34.6 17.2 189 3200
[76,] 42.9 17.6 196 4700
[77,] 36.7 18.8 187 3800
[78,] 35.1 19.4 193 4200
[79,] 37.3 17.8 191 3350
[80,] 41.3 20.3 194 3550
[81,] 36.3 19.5 190 3800
[82,] 36.9 18.6 189 3500
[83,] 38.3 19.2 189 3950
[84,] 38.9 18.8 190 3600
[85,] 35.7 18.0 202 3550
[86,] 41.1 18.1 205 4300
[87,] 34.0 17.1 185 3400
[88,] 39.6 18.1 186 4450
[89,] 36.2 17.3 187 3300
[90,] 40.8 18.9 208 4300
[91,] 38.1 18.6 190 3700
[92,] 40.3 18.5 196 4350
[93,] 33.1 16.1 178 2900
[94,] 43.2 18.5 192 4100
[95,] 35.0 17.9 192 3725
[96,] 41.0 20.0 203 4725
[97,] 37.7 16.0 183 3075
[98,] 37.8 20.0 190 4250
[99,] 37.9 18.6 193 2925
[100,] 39.7 18.9 184 3550
[101,] 38.6 17.2 199 3750
[102,] 38.2 20.0 190 3900
[103,] 38.1 17.0 181 3175
[104,] 43.2 19.0 197 4775
[105,] 38.1 16.5 198 3825
[106,] 45.6 20.3 191 4600
[107,] 39.7 17.7 193 3200
[108,] 42.2 19.5 197 4275
[109,] 39.6 20.7 191 3900
[110,] 42.7 18.3 196 4075
[111,] 38.6 17.0 188 2900
[112,] 37.3 20.5 199 3775
[113,] 35.7 17.0 189 3350
[114,] 41.1 18.6 189 3325
[115,] 36.2 17.2 187 3150
[116,] 37.7 19.8 198 3500
[117,] 40.2 17.0 176 3450
[118,] 41.4 18.5 202 3875
[119,] 35.2 15.9 186 3050
[120,] 40.6 19.0 199 4000
[121,] 38.8 17.6 191 3275
[122,] 41.5 18.3 195 4300
[123,] 39.0 17.1 191 3050
[124,] 44.1 18.0 210 4000
[125,] 38.5 17.9 190 3325
[126,] 43.1 19.2 197 3500
[127,] 36.8 18.5 193 3500
[128,] 37.5 18.5 199 4475
[129,] 38.1 17.6 187 3425
[130,] 41.1 17.5 190 3900
[131,] 35.6 17.5 191 3175
[132,] 40.2 20.1 200 3975
[133,] 37.0 16.5 185 3400
[134,] 39.7 17.9 193 4250
[135,] 40.2 17.1 193 3400
[136,] 40.6 17.2 187 3475
[137,] 32.1 15.5 188 3050
[138,] 40.7 17.0 190 3725
[139,] 37.3 16.8 192 3000
[140,] 39.0 18.7 185 3650
[141,] 39.2 18.6 190 4250
[142,] 36.6 18.4 184 3475
[143,] 36.0 17.8 195 3450
[144,] 37.8 18.1 193 3750
[145,] 36.0 17.1 187 3700
[146,] 41.5 18.5 201 4000
[147,] 46.1 13.2 211 4500
[148,] 50.0 16.3 230 5700
[149,] 48.7 14.1 210 4450
[150,] 50.0 15.2 218 5700
[151,] 47.6 14.5 215 5400
[152,] 46.5 13.5 210 4550
[153,] 45.4 14.6 211 4800
[154,] 46.7 15.3 219 5200
[155,] 43.3 13.4 209 4400
[156,] 46.8 15.4 215 5150
[157,] 40.9 13.7 214 4650
[158,] 49.0 16.1 216 5550
[159,] 45.5 13.7 214 4650
[160,] 48.4 14.6 213 5850
[161,] 45.8 14.6 210 4200
[162,] 49.3 15.7 217 5850
[163,] 42.0 13.5 210 4150
[164,] 49.2 15.2 221 6300
[165,] 46.2 14.5 209 4800
[166,] 48.7 15.1 222 5350
[167,] 50.2 14.3 218 5700
[168,] 45.1 14.5 215 5000
[169,] 46.5 14.5 213 4400
[170,] 46.3 15.8 215 5050
[171,] 42.9 13.1 215 5000
[172,] 46.1 15.1 215 5100
[173,] 47.8 15.0 215 5650
[174,] 48.2 14.3 210 4600
[175,] 50.0 15.3 220 5550
[176,] 47.3 15.3 222 5250
[177,] 42.8 14.2 209 4700
[178,] 45.1 14.5 207 5050
[179,] 59.6 17.0 230 6050
[180,] 49.1 14.8 220 5150
[181,] 48.4 16.3 220 5400
[182,] 42.6 13.7 213 4950
[183,] 44.4 17.3 219 5250
[184,] 44.0 13.6 208 4350
[185,] 48.7 15.7 208 5350
[186,] 42.7 13.7 208 3950
[187,] 49.6 16.0 225 5700
[188,] 45.3 13.7 210 4300
[189,] 49.6 15.0 216 4750
[190,] 50.5 15.9 222 5550
[191,] 43.6 13.9 217 4900
[192,] 45.5 13.9 210 4200
[193,] 50.5 15.9 225 5400
[194,] 44.9 13.3 213 5100
[195,] 45.2 15.8 215 5300
[196,] 46.6 14.2 210 4850
[197,] 48.5 14.1 220 5300
[198,] 45.1 14.4 210 4400
[199,] 50.1 15.0 225 5000
[200,] 46.5 14.4 217 4900
[201,] 45.0 15.4 220 5050
[202,] 43.8 13.9 208 4300
[203,] 45.5 15.0 220 5000
[204,] 43.2 14.5 208 4450
[205,] 50.4 15.3 224 5550
[206,] 45.3 13.8 208 4200
[207,] 46.2 14.9 221 5300
[208,] 45.7 13.9 214 4400
[209,] 54.3 15.7 231 5650
[210,] 45.8 14.2 219 4700
[211,] 49.8 16.8 230 5700
[212,] 49.5 16.2 229 5800
[213,] 43.5 14.2 220 4700
[214,] 50.7 15.0 223 5550
[215,] 47.7 15.0 216 4750
[216,] 46.4 15.6 221 5000
[217,] 48.2 15.6 221 5100
[218,] 46.5 14.8 217 5200
[219,] 46.4 15.0 216 4700
[220,] 48.6 16.0 230 5800
[221,] 47.5 14.2 209 4600
[222,] 51.1 16.3 220 6000
[223,] 45.2 13.8 215 4750
[224,] 45.2 16.4 223 5950
[225,] 49.1 14.5 212 4625
[226,] 52.5 15.6 221 5450
[227,] 47.4 14.6 212 4725
[228,] 50.0 15.9 224 5350
[229,] 44.9 13.8 212 4750
[230,] 50.8 17.3 228 5600
[231,] 43.4 14.4 218 4600
[232,] 51.3 14.2 218 5300
[233,] 47.5 14.0 212 4875
[234,] 52.1 17.0 230 5550
[235,] 47.5 15.0 218 4950
[236,] 52.2 17.1 228 5400
[237,] 45.5 14.5 212 4750
[238,] 49.5 16.1 224 5650
[239,] 44.5 14.7 214 4850
[240,] 50.8 15.7 226 5200
[241,] 49.4 15.8 216 4925
[242,] 46.9 14.6 222 4875
[243,] 48.4 14.4 203 4625
[244,] 51.1 16.5 225 5250
[245,] 48.5 15.0 219 4850
[246,] 55.9 17.0 228 5600
[247,] 47.2 15.5 215 4975
[248,] 49.1 15.0 228 5500
[249,] 46.8 16.1 215 5500
[250,] 41.7 14.7 210 4700
[ reached getOption("max.print") -- omitted 83 rows ]
row.names(penguins2) <-penguins2[,1]
Warning: Setting row names on a tibble is deprecated.Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length
Component 1 represents # of dimensions you have (x-axis, y-axis, z-axis & 4th-axes) and it rotates/interactions it based on categories [this will change the axes]
Individual Loadings
loadings(penguin_pca)
Loadings:
Comp.1 Comp.2 Comp.3 Comp.4
bill_length_mm 0.454 0.600 0.642 0.145
bill_depth_mm -0.399 0.796 -0.426 -0.160
flipper_length_mm 0.577 -0.236 -0.782
body_mass_g 0.550 -0.592 0.585
Comp.1 Comp.2 Comp.3 Comp.4
SS loadings 1.00 1.00 1.00 1.00
Proportion Var 0.25 0.25 0.25 0.25
Cumulative Var 0.25 0.50 0.75 1.00
levels(penguins2$species)
[1] "Adelie" "Chinstrap" "Gentoo"
To get PCA scores
penguin_pca$scores
Comp.1 Comp.2 Comp.3 Comp.4
[1,] -1.85359302 0.032069377 -0.2349016613 0.528396625
[2,] -1.31625406 -0.443526765 -0.0274700760 0.401726631
[3,] -1.37660509 -0.161230478 0.1896892637 -0.528661902
[4,] -1.88528838 -0.012351235 -0.6288726870 -0.472893203
[5,] -1.91998074 0.817598126 -0.7010513885 -0.196416463
[6,] -1.77302031 -0.366222957 0.0284604590 0.505368579
[7,] -0.81849625 0.501243084 -1.3350037341 0.348259556
[8,] -1.79895773 -0.245393945 0.6265541188 0.215177636
[9,] -1.95614892 0.998282895 -1.0406150295 -0.210615154
[10,] -1.56952316 0.578081948 -2.0492974540 -0.263635364
[11,] -1.74800122 -0.610244291 -0.3648455682 0.275254393
[12,] -1.57577371 0.086835726 -0.3623210792 -0.507079122
[13,] -0.80472019 1.293555921 -1.0898644424 0.107519267
[14,] -2.35017809 -0.645191072 -0.4607707684 -0.048857069
[15,] -1.00498645 1.972422509 -0.5798770729 0.084976671
[16,] -2.40824844 -0.308968645 0.0744441020 0.662978776
[17,] -2.11369825 -0.136493144 -0.2723621056 0.437971409
[18,] -1.85705729 -0.109144060 -0.8914621801 -0.007995218
[19,] -1.50501042 -0.289127997 -0.4255462669 0.475198437
[20,] -1.58113786 -0.603932517 0.0346978179 0.734644053
[21,] -1.92846722 -0.297394981 -0.8633540656 0.112204664
[22,] -1.76295054 0.138259762 0.0767052076 0.319209942
[23,] -1.70361341 -0.187802307 0.4066389913 -0.104556344
[24,] -2.71417458 -0.201106317 0.2389821840 0.571190457
[25,] -1.68232816 0.285542330 -0.2438102280 0.750672188
[26,] -1.87994963 -0.782580998 0.6638711756 0.505701001
[27,] -1.91081367 -0.406695073 -0.3883180768 0.803391776
[28,] -1.65683258 -0.328286332 0.2201618206 -0.106165116
[29,] -1.51840291 0.326408242 -0.2274268406 0.501437178
[30,] -1.44646684 -0.987685263 -0.1078452205 -0.496481192
[31,] -1.44062410 1.059095864 -1.2903086836 -0.211391293
[32,] -1.63466140 0.548223391 -0.8507480012 0.057249680
[33,] -1.73335112 0.272394506 0.3372327369 0.537510640
[34,] -2.40765908 0.067345151 -0.2101338390 0.112476774
[35,] -1.13764744 0.357809820 -0.9521566896 1.001340496
[36,] -2.29657080 -0.593801144 0.0354812471 0.023993523
[37,] -0.97184877 0.117509989 -0.3164908762 -0.075207890
[38,] -2.30890668 -0.449404139 -0.1623082340 -0.289830288
[39,] -0.57840195 1.054586456 -0.5945824193 0.214535227
[40,] -2.01067992 -0.997271019 0.3923067603 -0.149971631
[41,] -0.88026262 0.212079200 -0.9751464709 0.648796919
[42,] -1.92925587 0.342881528 0.1577848524 0.264948453
[43,] -1.78298528 -0.657410953 -0.3574537623 -0.210018964
[44,] -1.40940140 1.438260966 -0.8628557816 0.142277387
[45,] -1.57392895 -0.339592411 0.1401266848 0.161823273
[46,] -1.14654389 0.278170592 -0.6834333861 0.547546732
[47,] -1.86608339 -0.767327681 -0.4751171379 -0.236604598
[48,] -0.78673386 0.711147080 -0.6082512171 -0.303064057
[49,] -2.44789222 -0.794851225 -0.1218556728 -0.498499722
[50,] -1.26418254 0.243767425 -0.0745246189 0.002543992
[51,] -1.54901519 -0.481769739 0.0760396714 0.198497399
[52,] -1.22044841 0.247154032 -0.3193049336 -0.074021804
[53,] -2.25876529 -1.189622965 0.5763198147 -0.024441402
[54,] -1.52359256 0.034535966 -0.7173315591 -0.270413670
[55,] -2.01615696 -1.125897262 0.1289464113 -0.075450514
[56,] -1.13641794 1.313283236 -1.2103418754 0.082119719
[57,] -1.57091360 -0.833767737 -0.0003422234 0.294177168
[58,] -0.92743183 0.082527261 -0.2976685297 0.225746271
[59,] -2.24489579 -0.996917698 0.4056666843 -0.235393033
[60,] -0.91366065 0.046992829 -0.1219278693 0.182582152
[61,] -1.34180687 -1.408162573 -0.0589005898 -0.437169086
[62,] -1.24076891 0.450049108 -0.4618830617 0.412424419
[63,] -1.80093377 -1.232829956 0.2066916214 -0.397843538
[64,] -0.59202566 0.685886654 -0.8707826466 0.102391897
[65,] -2.11142026 -0.472533742 -1.0002169890 -0.256856415
[66,] -1.26934357 -0.005466398 -0.3615689854 0.174933474
[67,] -1.02609887 -0.533157420 0.0428999873 -0.319925306
[68,] -0.40447877 0.894152813 -0.0541870716 0.087108392
[69,] -1.57243857 -0.850558396 -0.5136613869 -0.008978328
[70,] -0.58666260 0.411120851 -0.3604145329 0.224356728
[71,] -0.94042944 -0.540033074 0.2564366830 0.135633405
[72,] -1.92733875 0.122172496 -0.7710571606 0.362408603
[73,] -1.45634863 -1.356000198 0.0328842243 0.182033357
[74,] -0.93751617 0.553350680 -0.3887132911 -0.024851361
[75,] -1.96939557 -1.118922417 -0.1697294034 -0.316239385
[76,] -0.04683282 0.100901565 -0.5018159035 0.571576618
[77,] -1.79183530 -0.184002791 -0.6769701737 0.157556898
[78,] -1.52578740 -0.076399251 -1.3907790578 0.021849643
[79,] -1.68181280 -0.564107174 -0.1261078882 -0.295924034
[80,] -1.59639832 0.908101945 -0.3946417816 -0.415078234
[81,] -1.84348437 0.056709917 -0.9262326489 -0.077635115
[82,] -1.85729280 -0.270705718 -0.4230718864 -0.150780390
[83,] -1.55507132 0.168921592 -0.7194680394 0.164892744
[84,] -1.62210133 0.040034131 -0.3215244287 -0.097029300
[85,] -1.26523377 -0.635421240 -0.6904504308 -0.823930721
[86,] -0.20039388 0.071188634 -0.6793241349 -0.310719775
[87,] -2.02709537 -1.207997401 -0.2983882606 0.044893285
[88,] -1.00562068 -0.087279300 -0.6456810644 0.820149350
[89,] -1.87080090 -0.893881281 -0.0429811675 -0.097358865
[90,] -0.26402770 0.363384244 -0.9384754387 -0.551385571
[91,] -1.57962369 -0.119371375 -0.4459434375 -0.029311487
[92,] -0.68482350 0.146252964 -0.7450419331 0.174737159
[93,] -2.52929467 -1.762281613 0.2983518103 0.129821325
[94,] -0.77962599 0.439581247 -0.1523402596 0.293539877
[95,] -1.59563939 -0.740347063 -0.7112540408 -0.148374347
[96,] -0.38617540 0.869122059 -1.3815922089 -0.047071773
[97,] -1.80101966 -1.278444819 0.6481092793 0.108124873
[98,] -1.51265850 0.466837670 -1.1892072983 0.248828929
[99,] -2.00243546 -0.213819033 0.0503023889 -0.765852564
[100,] -1.85740516 0.161221992 -0.1110276495 0.215008721
[101,] -0.84881085 -0.622812685 -0.2725716316 -0.368699022
[102,] -1.71870377 0.477518186 -0.8845467063 0.004937486
[103,] -1.98479380 -0.820882689 0.4387649696 0.221911517
[104,] -0.21353466 0.708300149 -0.8417585173 0.464363571
[105,] -0.73824008 -0.954490503 -0.2181458163 -0.214647083
[106,] -0.64487503 1.479361613 -0.6108640296 0.630448418
[107,] -1.48219852 -0.354236566 0.2545966984 -0.444822833
[108,] -0.73994061 0.753287890 -0.6997045208 0.033508069
[109,] -1.70321099 0.915253811 -0.8882744639 -0.070646162
[110,] -0.63280816 0.302917228 -0.2169432381 0.054822896
[111,] -1.84273238 -0.789182569 0.5819006733 -0.355919928
[112,] -1.60946727 0.572883733 -1.1585541611 -0.653447361
[113,] -1.73484802 -1.064730972 -0.1073873188 -0.161648054
[114,] -1.62792299 0.174301453 0.1999119118 -0.166383577
[115,] -1.95305685 -0.948638014 0.0890719593 -0.198308960
[116,] -1.66339271 0.306844796 -0.7406360661 -0.729994048
[117,] -1.82836539 -0.565972121 0.5678134008 0.757111386
[118,] -0.67085459 0.224468852 -0.3672406022 -0.476709049
[119,] -1.88191000 -1.594864662 0.3433947042 -0.136018610
[120,] -0.87699928 0.349638746 -0.6110344714 -0.280105915
[121,] -1.56785175 -0.487347294 0.1488964877 -0.294322112
[122,] -0.61991752 0.192001813 -0.5068659092 0.242420910
[123,] -1.60358507 -0.689218352 0.4463019344 -0.411966916
[124,] 0.07018089 0.333984567 -0.1682345245 -0.720380972
[125,] -1.66069875 -0.394507052 0.0287029773 -0.234456914
[126,] -1.13411289 0.657034153 0.0415474793 -0.481760436
[127,] -1.68043855 -0.320534232 -0.4806643410 -0.368815057
[128,] -0.70838734 -0.148385164 -1.2171086476 0.023568081
[129,] -1.68833943 -0.551677888 0.0236154471 0.019658835
[130,] -0.97035514 -0.216004046 -0.0019437648 0.285344477
[131,] -1.88183815 -0.889082390 -0.1323725075 -0.443985421
[132,] -1.10935310 0.749111595 -0.8947773093 -0.454253495
[133,] -1.65603365 -1.121194594 0.1845328627 0.173444263
[134,] -0.80493412 -0.173395579 -0.5614993590 0.302489564
[135,] -1.18214807 -0.523204908 0.2961621018 -0.237292684
[136,] -1.36523239 -0.434095818 0.3675952302 0.155013118
[137,] -1.97590114 -2.096744245 0.0315182628 -0.297658915
[138,] -1.02176382 -0.479069974 0.1880643363 0.188109106
[139,] -1.67693429 -1.001892051 0.3311686937 -0.525003382
[140,] -1.76540033 0.013221756 -0.2405506415 0.229507985
[141,] -1.11219725 0.053843874 -0.7213062471 0.399901662
[142,] -2.06481174 -0.389108766 -0.3123093029 0.118713766
[143,] -1.55660384 -0.695834197 -0.4201502001 -0.481272180
[144,] -1.34524467 -0.348806583 -0.4603769023 -0.127895970
[145,] -1.57336339 -0.958805742 -0.3175980725 0.204475211
[146,] -0.61830340 0.246934847 -0.4306022445 -0.327271339
[147,] 1.59356859 -1.341795728 0.7216766759 0.030873935
[148,] 2.89205390 0.464090012 -0.6944660890 -0.306550426
[149,] 1.55157173 -0.696759932 0.8863745985 0.046321767
[150,] 2.62068561 0.013723319 -0.2538154127 0.453430729
[151,] 2.23455895 -0.563287236 -0.1132144676 0.396018085
[152,] 1.55889027 -1.172013776 0.6838475283 0.109349077
[153,] 1.45637702 -0.823329213 0.1153440662 0.116576029
[154,] 2.02554963 -0.355648737 -0.3126368365 -0.061919459
[155,] 1.16950299 -1.578917638 0.4562481741 -0.020797935
[156,] 1.81451188 -0.310575391 -0.2182450616 0.119754394
[157,] 1.28618821 -1.695400271 -0.1594576306 -0.206581987
[158,] 2.16995116 0.253134960 -0.4222366808 0.356325763
[159,] 1.66843953 -1.189783321 0.3817938970 -0.084288071
[160,] 2.50595964 -0.392893307 -0.3381921739 0.848155795
[161,] 1.03819687 -0.836838135 0.6208715802 -0.253242371
[162,] 2.52237724 0.153089665 -0.5379832461 0.559118608
[163,] 0.91148075 -1.704680397 0.4487569003 -0.301172786
[164,] 3.08806126 -0.015907257 -0.8401489185 0.700860650
[165,] 1.46071532 -0.776714255 0.2648705743 0.257731106
[166,] 2.45853762 -0.201291445 -0.1950086762 -0.051032395
[167,] 2.81995631 -0.328714403 -0.0353862428 0.531938971
[168,] 1.75334566 -0.876120401 -0.1129783445 0.038667490
[169,] 1.37704625 -0.780126191 0.5270828658 -0.248688163
[170,] 1.62341756 -0.213079172 -0.2900987733 0.001210565
[171,] 1.85465371 -1.684814420 -0.0686654454 0.094031942
[172,] 1.78304339 -0.513745958 -0.1988446060 0.089180675
[173,] 2.32062326 -0.315071902 -0.3819542242 0.542477550
[174,] 1.57198405 -0.656470332 0.6738346178 0.125846699
[175,] 2.58027529 0.040776239 -0.1988132596 0.224461717
[176,] 2.23324413 -0.283702741 -0.3294490937 -0.177238602
[177,] 1.17069843 -1.281415156 0.0033792361 0.119015362
[178,] 1.45779016 -0.874674010 -0.0148150986 0.522045203
[179,] 3.78701834 1.836015387 0.0259209120 0.146270932
[180,] 2.33349180 -0.298646309 0.0979600949 -0.049690133
[181,] 2.14182216 0.255556267 -0.4932283607 -0.008481200
[182,] 1.59133864 -1.480424422 -0.1633554929 0.112655586
[183,] 1.46271619 0.206122550 -1.0531652424 -0.249352525
[184,] 1.11168166 -1.426162233 0.5489718459 0.001063615
[185,] 1.75972697 0.035865574 -0.0887551819 0.682453101
[186,] 0.70989154 -1.566604094 0.6687488536 -0.332516565
[187,] 2.71361148 0.296581645 -0.5922143371 -0.013402080
[188,] 1.24766590 -1.246707231 0.6833377467 -0.120622445
[189,] 1.89611421 -0.202401218 0.4753572955 -0.120040069
[190,] 2.58249171 0.339509176 -0.3036532099 0.077206232
[191,] 1.76453362 -1.292626014 -0.1196844123 -0.136892736
[192,] 1.15532939 -1.153251761 0.7371587302 -0.204291638
[193,] 2.60359333 0.326484463 -0.2438662115 -0.199507546
[194,] 1.96619306 -1.375315362 0.0834929746 0.315414369
[195,] 1.70292714 -0.310211734 -0.6035250877 0.153770437
[196,] 1.63023914 -0.849052487 0.3232317817 0.273246100
[197,] 2.52824538 -0.633769450 0.0685502714 0.100367351
[198,] 1.15735133 -0.975741632 0.4346202685 -0.110144367
[199,] 2.47953715 -0.119944640 0.1983594951 -0.427837195
[200,] 1.90404533 -0.771411353 0.1132635463 -0.100456128
[201,] 1.80265515 -0.515867852 -0.4407921010 -0.280206936
[202,] 0.99999485 -1.331427058 0.4972729927 -0.065011363
[203,] 1.89119896 -0.627629575 -0.2585404286 -0.270745548
[204,] 0.93091910 -1.140164212 0.1863460101 -0.020674379
[205,] 2.77838403 0.086397319 -0.2192291925 0.011587521
[206,] 1.07656958 -1.216553531 0.7690218627 -0.089722194
[207,] 2.21598059 -0.562234490 -0.3921871419 -0.081715488
[208,] 1.47355252 -1.110593355 0.5460128412 -0.277039706
[209,] 3.37817705 0.689442994 -0.0386535780 -0.235676050
[210,] 1.83216652 -0.947529000 0.1876661551 -0.359998860
[211,] 2.77396146 0.644562815 -0.8262745946 -0.352529284
[212,] 2.89794904 0.377737157 -0.7883709304 -0.183112157
[213,] 1.68225824 -1.199923878 -0.0998299295 -0.477022930
[214,] 2.82297979 -0.002514949 -0.1020943607 0.099837362
[215,] 1.73822780 -0.411243001 0.2517968820 -0.170552774
[216,] 1.88543725 -0.285343544 -0.2994447079 -0.351489667
[217,] 2.10338085 -0.077983097 -0.1612492724 -0.230913899
[218,] 2.02796808 -0.580915426 -0.1941530395 0.085179370
[219,] 1.59601675 -0.558889916 0.1356338140 -0.241474911
[220,] 2.90496724 0.198243239 -0.8678279570 -0.246651648
[221,] 1.49289256 -0.774316869 0.6299957418 0.171246214
[222,] 2.77638908 0.609393449 -0.6171290897 0.499629777
[223,] 1.73279991 -1.172343176 0.2343707570 -0.083551592
[224,] 2.35528428 -0.002138394 -1.3468098137 0.130650040
[225,] 1.70570972 -0.473358039 0.6842810220 0.039935259
[226,] 2.69998725 0.427945010 0.0871080008 0.137930023
[227,] 1.61251537 -0.610214911 0.3889994769 0.059328963
[228,] 2.48664340 0.266357334 -0.2490282583 -0.193284064
[229,] 1.58421835 -1.206558986 0.2496827066 0.076104055
[230,] 2.60478500 0.946598161 -0.7095477675 -0.327572783
[231,] 1.48255754 -1.140270621 -0.0475673172 -0.456913588
[232,] 2.65819078 -0.286338576 0.4100931985 0.278429004
[233,] 1.84514307 -0.827905113 0.4202988503 0.219864045
[234,] 2.82194749 0.964088245 -0.4885612022 -0.416729460
[235,] 1.94077693 -0.413378481 0.0473262843 -0.142180870
[236,] 2.62497748 1.000478450 -0.3543114285 -0.419531459
[237,] 1.49201527 -0.857170341 0.1686945693 0.035129012
[238,] 2.60960622 0.320912437 -0.5719661997 -0.004676691
[239,] 1.51912978 -0.875767080 -0.0996184205 -0.046753911
[240,] 2.57359526 0.259869947 -0.0349299064 -0.376587531
[241,] 1.83678033 0.116188362 0.1497856708 -0.063153117
[242,] 2.08569057 -0.646771800 0.0510666206 -0.403652536
[243,] 1.29687923 -0.594513353 0.7754047121 0.532351673
[244,] 2.42913431 0.621116375 -0.1928012220 -0.341432684
[245,] 1.99672542 -0.312558491 0.2217179796 -0.244193976
[246,] 3.08946906 1.385699786 -0.0444990538 -0.167589013
[247,] 1.70781430 -0.242760558 -0.0640372566 -0.005006547
[248,] 2.86192618 -0.181068897 -0.3379080453 -0.258446028
[249,] 1.91173444 0.006149393 -0.6274264661 0.317353668
[250,] 1.01903506 -1.199453811 -0.2511966283 -0.006767678
[ reached getOption("max.print") -- omitted 83 rows ]
ADD LESSON STUFF FROM LAST CLASS
Hard method:
rand
[1] 12 54 98 65 38
To get cumulative sum:
sum(rand)
[1] 267
length(rand)
[1] 5
To get the mean of your data:
avg.rand
[1] 53.4
Another way to get the average:
mean(rand)
[1] 53.4
To sort:
sort(rand)
[1] 12 38 54 65 98
min(rand)
[1] 12
max(rand)
[1] 98
cumsum(rand)
[1] 12 66 164 229 267
To get differences:
diff(rand)
[1] 42 44 -33 -27
Slicing/Indexing #’s out:
rand[2]
[1] 54
Transforming #’s: adding, multiplying, etc
Importing Data, Most Useful: Take a list of numbers, don’t need to add comma’s and just paste in data. Press ‘enter’ twice to finish adding data
peds
[1] 2 3 16 23 14 12 4 13 2 0 0 0 6 28 31 14 4 8 2 5
length(beer)
[1] 25
You cannot use “barplot(beer)” command right away. It will just show you every # in its own column
table(beer)
beer
1 2 3 4
10 4 8 3
This is just for counts/frequency:
For proportion:
stem(peds)
The decimal point is 1 digit(s) to the right of the |
0 | 000222344568
1 | 23446
2 | 38
3 | 1
mean(peds)
[1] 9.35
Using Columns in Data frame:
mean(gapminder2$lifeExp)
[1] 59.47444
median(peds)
[1] 5.5
median(gapminder2$lifeExp)
[1] 60.7125
Must know exact column headers using the “with” command
with(gapminder2, median(lifeExp))
[1] 60.7125
Frequency of measurement occurring:
which(table(peds) == max(table(peds)))
0 2
1 2
range(peds)
[1] 0 31
Will give you a summary of the range:
diff(range(peds))
[1] 31
var(peds)
[1] 87.60789
Standard Deviation: sqrt of variance
sd(peds)
[1] 9.359909
IQR(peds)
[1] 12
The number of standard deviations away from the mean
scale(peds)
[,1]
[1,] -0.7852641
[2,] -0.6784254
[3,] 0.7104770
[4,] 1.4583475
[5,] 0.4967997
[6,] 0.2831224
[7,] -0.5715868
[8,] 0.3899611
[9,] -0.7852641
[10,] -0.9989413
[11,] -0.9989413
[12,] -0.9989413
[13,] -0.3579095
[14,] 1.9925408
[15,] 2.3130567
[16,] 0.4967997
[17,] -0.5715868
[18,] -0.1442322
[19,] -0.7852641
[20,] -0.4647481
attr(,"scaled:center")
[1] 9.35
attr(,"scaled:scale")
[1] 9.359909
summary(peds)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 2.00 5.50 9.35 14.00 31.00
Modify # of breaks:
hist(peds, breaks="scott", prob=TRUE)
lines(density(peds))
boxplot(spid.gen)
Error in x[floor(d)] + x[ceiling(d)] :
non-numeric argument to binary operator
Changing plot characters using pch
To use a linear model, define general regression variable using lm():
gen.reg <- lm(spid.gen$left.bulb ~ spid.gen$right.bulb)
gen.reg
Call:
lm(formula = spid.gen$left.bulb ~ spid.gen$right.bulb)
Coefficients:
(Intercept) spid.gen$right.bulb
47.2805 0.7545
Using summary(gen.reg) will result in an ANOVA test:
summary(gen.reg)
Call:
lm(formula = spid.gen$left.bulb ~ spid.gen$right.bulb)
Residuals:
Min 1Q Median 3Q Max
-28.973 -16.272 -7.931 12.167 48.620
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 47.2805 27.0506 1.748 0.10600
spid.gen$right.bulb 0.7545 0.1974 3.823 0.00243 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 24.67 on 12 degrees of freedom
Multiple R-squared: 0.5491, Adjusted R-squared: 0.5115
F-statistic: 14.61 on 1 and 12 DF, p-value: 0.002427
Using abline will give you your linear regression line into your plot:
plot(spid.gen$left.bulb ~ spid.gen$right.bulb, pch = as.character(spid.gen$habitat))
abline(gen.reg)
Cor.gen will give you your R-value, to get R^2, use cor.gen^2:
cor.gen <- with(spid.gen, cor(left.bulb, right.bulb))
cor.gen
[1] 0.7410186
cor.gen^2
[1] 0.5491086
spearman.cor.gen^2
[1] 0.5274774
Residuals are the difference between a value and the line of best fit (LOBF)
residuals(gen.reg)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
9.026729 -17.942400 21.026729 -10.116375 -21.410662 -28.973271 -5.901718 48.620209 -11.259480 -2.241592 40.566544 13.213688 -24.648113 -9.960288
log.left.bulb <- log10(spid.gen$left.bulb)
chisq.test(obs, p=exp)
Chi-squared test for given probabilities
data: obs
X-squared = 21.691, df = 5, p-value = 0.0005993
obs_weighted <- c(4,15,6,15,18,2)
obs_fair <- c(10,10,10,10,10,10)
exp <- c(0.16,0.17,0.16,0.17,0.17,0.17)
This will be significant:
chisq.test(obs_weighted, p=exp)
Chi-squared test for given probabilities
data: obs_weighted
X-squared = 21.691, df = 5, p-value = 0.0005993
This is not significant since p=value is 1:
chisq.test(obs_fair, p=exp)
Chi-squared test for given probabilities
data: obs_fair
X-squared = 0.04902, df = 5, p-value = 1
Null hypothesis (H0): true mean is equal to zero, but you can set true mean to other values with “mu=”
1 Sample T-Test: Student T-Test (2-Tail Test)
t.test(spid.gen$carapace.length)
One Sample t-test
data: spid.gen$carapace.length
t = 27.953, df = 13, p-value = 5.373e-13
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
26.69279 31.16436
sample estimates:
mean of x
28.92857
mean(spid.gen$carapace.length)
[1] 28.92857
t.test(spid.gen$carapace.length, mu=29)
One Sample t-test
data: spid.gen$carapace.length
t = -0.069019, df = 13, p-value = 0.946
alternative hypothesis: true mean is not equal to 29
95 percent confidence interval:
26.69279 31.16436
sample estimates:
mean of x
28.92857
2 Sample T-Test:
habitat.t2
Welch Two Sample t-test
data: spid.gen$left.bulb by spid.gen$habitat
t = -4.9442, df = 11.485, p-value = 0.0003868
alternative hypothesis: true difference in means between group A and group B is not equal to 0
95 percent confidence interval:
-80.38903 -31.03954
sample estimates:
mean in group A mean in group B
119.7143 175.4286
habitat.t2 <- t.test(spid.gen$left.bulb~spid.gen$habitat)
habitat.t2
Welch Two Sample t-test
data: spid.gen$left.bulb by spid.gen$habitat
t = -4.9442, df = 11.485, p-value = 0.0003868
alternative hypothesis: true difference in means between group A and group B is not equal to 0
95 percent confidence interval:
-80.38903 -31.03954
sample estimates:
mean in group A mean in group B
119.7143 175.4286
1 Sample Test: Is one less than the other? (1-Tailed)
habitat.tless
One Sample t-test
data: spid.gen$left.bulb
t = 15.643, df = 13, p-value = 1
alternative hypothesis: true mean is less than 0
95 percent confidence interval:
-Inf 164.2781
sample estimates:
mean of x
147.5714
Conclusion: B is not less than A
1 Sample Test: Is one greater than the other? (1-Tailed)
habitat.tgreater <- t.test(spid.gen$left.bulb, alternative = "greater")
habitat.tgreater
One Sample t-test
data: spid.gen$left.bulb
t = 15.643, df = 13, p-value = 4.109e-10
alternative hypothesis: true mean is greater than 0
95 percent confidence interval:
130.8648 Inf
sample estimates:
mean of x
147.5714
gen.lm
Call:
lm(formula = spid.gen$left.bulb ~ spid.gen$habitat)
Coefficients:
(Intercept) spid.gen$habitatB
119.71 55.71
summary(gen.lm)
Call:
lm(formula = spid.gen$left.bulb ~ spid.gen$habitat)
Residuals:
Min 1Q Median 3Q Max
-29.714 -16.250 2.429 20.036 23.286
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 119.714 7.968 15.024 3.82e-09 ***
spid.gen$habitatB 55.714 11.269 4.944 0.00034 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 21.08 on 12 degrees of freedom
Multiple R-squared: 0.6707, Adjusted R-squared: 0.6433
F-statistic: 24.45 on 1 and 12 DF, p-value: 0.0003397
anova(gen.lm)
Analysis of Variance Table
Response: spid.gen$left.bulb
Df Sum Sq Mean Sq F value Pr(>F)
spid.gen$habitat 1 10864.3 10864.3 24.445 0.0003397 ***
Residuals 12 5333.1 444.4
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
gen.anova
Call:
aov(formula = spid.gen$left.bulb ~ spid.gen$habitat)
Terms:
spid.gen$habitat Residuals
Sum of Squares 10864.286 5333.143
Deg. of Freedom 1 12
Residual standard error: 21.08147
Estimated effects may be unbalanced
summary(gen.anova)
Df Sum Sq Mean Sq F value Pr(>F)
spid.gen$habitat 1 10864 10864 24.45 0.00034 ***
Residuals 12 5333 444
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Note: You get the same output
summary(gapminder2.lifeexp.continent.anova)
Df Sum Sq Mean Sq F value Pr(>F)
gapminder2$continent 4 139343 34836 408.7 <2e-16 ***
Residuals 1699 144805 85
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Performing a Post Hoc Test: Tukey Test
gapminder2.tukey.two.way <- TukeyHSD(gapminder2.lifeexp.continent.anova)
gapminder2.tukey.two.way
Supplying to model other factors that might explain the differences
boxplot(spid.gen$left.bulb~spid.gen$habitat)
Use a covariate by adding *factor_you_want_to_control
gen.ancova <- lm(spid.gen$left.bulb~spid.gen$habitat*spid.gen$carapace.length)
summary(gen.ancova)
gapminder2.lifeexp.continent_GDP.ancova <- aov(gapminder2$lifeExp~gapminder2$continent+gapminder2$gdpPercap)
summary(gapminder2.lifeexp.continent_GDP.ancova)
install.packages("multcomp")
library(multcomp)
#posthoc.gapminder2.ancova <- glht(gapminder2.lifeexp.continent_GDP.ancova, linfct=mcp(technique="Tukey")) (isn't working)
sp.matrix <- with(spid.gen, cbind(left.bulb, right.bulb, carapace.length, leg4.length))
sp.matrix
sp.pca <- princomp(sp.matrix, cor=TRUE)
summary(sp.pca)
loadings(sp.pca)
biplot(sp.pca)
install.packages("ggfortify", dependencies = TRUE)
sp.pca$scores
library(ggfortify)
plot(1:25, rep(0.25,25), pch=1:25, col=1:25, ylim=c(0,6), cex=2, ylab="Line types (lty) 1 to 6", xlab="Plotting character (pch) 1 to 25 and colours (col) 1 to 8", main="Line types (lty), plotting characters (pch),\nand colors (col) for plot and xyplot", lab=c(25,7,2))
points(1:8, rep(0.5,8), pch=20, col=1:8, cex=4)
abline(h=1:6, lty=1:6, lwd=5, col=1:8)
x <- 5
if(x > 0){
print("Positive #")}
[1] "Positive #"
x <- -5
if(x > 0){
print("Positive #")
} else if (x < 0) {
print("Negative #")
} else
print("Zero")
%% Represents division, and == means no remainder “if its true”, “if its odd”
a <- c(5,7,2,9)
ifelse(a %% 2 == 0, "even", "odd")
[1] "odd" "odd" "even" "odd"
For each of these values in Z, divide them by 2, if they don’t have a remainder, it will tell you how many even numbers
z <- c(2,5,3,9,8,11,6)
count <- 0
for(val in z) {
if(val %% 2 == 0) count = count + 1
}
print(count)
[1] 3
while (i < 5) {
print(i)
i = i +1
}
[1] 1
[1] 2
[1] 3
[1] 4
Breaks the loop
x <- 1:5
for (val in x){
if (val == 3){
break
}
print(val)
}
[1] 1
[1] 2
If print(val) is placed before the break, it will print the value 3, then break the loop
x <- 1:5
for (val in x){
if (val == 3){
print(val)
break
}
#print(val)
}
[1] 3
x <- 1:5
for (val in x){
print(val)
if (val == 3){
#print(val)
break
}
#print(val)
}
[1] 1
[1] 2
[1] 3
If the value is equal to 3, it will skip that value and move onto the next one
for (val in x){
if (val == 3) {
next
}
print(val)
}
[1] 1
[1] 2
[1] 4
[1] 5
x <- 1
repeat{
print(x)
x = x+1
if (x == 20) {
break
}
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12
[1] 13
[1] 14
[1] 15
[1] 16
[1] 17
[1] 18
[1] 19
matrix_B
[,1] [,2]
[1,] 2 1
[2,] 4 5
[3,] 3 7
Transforming Data
t(matrix_B)
[,1] [,2] [,3]
[1,] 2 4 3
[2,] 1 5 7
matrix_C <- matrix(
c(7,4,2),
nrow=3,
ncol=1
)
matrix_C
[,1]
[1,] 7
[2,] 4
[3,] 2
Putting the Matrices/Data.frame together
matrix_BC
[,1] [,2] [,3]
[1,] 2 1 7
[2,] 4 5 4
[3,] 3 7 2
Deconstructing the Matrices
c(matrix_BC)
[1] 2 4 3 1 5 7 7 4 2
Each tiime you run the script, you will get different numbers
rnorm(# samples, average, variance as standard deviation) sample(what you want to sample, size of sample, replace = FALSE, prob = NULL)
ndist
[1] 55.9542490 26.3697544 89.4133606 30.4705159 77.4904339 45.1497019 42.5528411 55.8054149 66.2090974
[10] 81.9794886 58.9407723 54.2343070 54.3273391 33.0817729 75.3197815 26.2599278 70.6045629 38.6222709
[19] 0.8912044 3.9895768 68.4496573 75.7090393 69.6527897 0.4292658 18.1040327 32.2248827 45.7944139
[28] 65.8302703 85.7585557 31.2537956 18.9440611 54.0444611 44.9547697 37.9902935 71.7696526 70.4014492
[37] 40.9347399 62.3939766 65.3758079 42.6461025 50.0646070 76.2433791 29.1494626 24.2207133 51.3884549
[46] 35.1167009 42.0008419 56.9603999 72.9600210 57.1145944 89.9174185 35.6649101 39.9447309 24.8010554
[55] 54.4779804 89.6292238 67.3184735 40.5109672 64.2697703 60.9038885 73.4688038 52.7006923 -2.0313551
[64] 23.9281731 60.7924906 46.2207658 60.4088549 34.5986970 62.5923770 109.2151936 47.8052044 10.5844283
[73] 38.7855412 81.5907308 37.6361760 45.7837776 64.5194886 29.2113467 69.6984450 46.8443136 64.7202652
[82] 62.6272938 65.9904559 31.4220989 56.3600481 92.1638530 59.7632149 40.3388356 6.6850950 31.8363553
[91] 50.8770143 56.6747157 47.7469551 110.4550490 42.0540827 47.5342241 48.4177596 44.9337455 69.7509705
[100] 51.5046615
sample(ndist, 10, replace = FALSE, prob = NULL)
[1] 92.163853 33.081773 64.519489 67.318474 6.685095 65.375808 32.224883 42.552841 56.360048 50.877014
test <- 1:100
test
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
[30] 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
[59] 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
[88] 88 89 90 91 92 93 94 95 96 97 98 99 100
No repeats:
sort(sample(test, 50, replace = FALSE, prob = NULL))
[1] 1 2 3 4 6 9 10 11 14 16 20 21 22 23 24 25 27 30 32 34 36 38 40 43 44 52 54 62 65
[30] 67 68 72 73 74 75 78 80 83 85 86 88 91 92 94 95 96 97 98 99 100
Now you can have repeats:
sort(sample(test, 50, replace = TRUE, prob = NULL))
[1] 1 3 8 11 13 21 22 27 29 30 31 31 37 37 38 42 44 47 53 57 57 59 59 60 61 64 66 66 68
[30] 69 69 70 72 73 74 74 76 78 79 79 83 84 88 90 91 91 92 96 97 100